| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\CPT; |
| 4 |
|
| 5 |
use FluentCart\Api\StoreSettings; |
| 6 |
use FluentCart\App\Models\DynamicModel; |
| 7 |
use FluentCart\Framework\Support\Arr; |
| 8 |
use FluentCart\Framework\Support\Str; |
| 9 |
|
| 10 |
class Pages |
| 11 |
{ |
| 12 |
|
| 13 |
protected static array $cachedPages = []; |
| 14 |
|
| 15 |
public function pages(): array |
| 16 |
{ |
| 17 |
// Lets addons register their own generatable pages ('key' => title + content), |
| 18 |
// so the Pages Setup "+" create button and onboarding can build them. |
| 19 |
return apply_filters('fluent_cart/generatable_pages', $this->corePages()); |
| 20 |
} |
| 21 |
|
| 22 |
public function corePages(): array |
| 23 |
{ |
| 24 |
return [ |
| 25 |
'checkout' => [ |
| 26 |
'title' => 'Checkout', |
| 27 |
'content' => '[fluent_cart_checkout]' |
| 28 |
], |
| 29 |
'cart' => [ |
| 30 |
'title' => 'Cart', |
| 31 |
'content' => '[fluent_cart_cart]' |
| 32 |
], |
| 33 |
'receipt' => [ |
| 34 |
'title' => 'Receipt', |
| 35 |
'content' => '[fluent_cart_receipt]' |
| 36 |
], |
| 37 |
'shop' => [ |
| 38 |
'title' => 'Shop', |
| 39 |
'content' => static::getShopPageContent() |
| 40 |
], |
| 41 |
'customer_profile' => [ |
| 42 |
'title' => 'Account', |
| 43 |
'content' => '<!-- wp:fluent-cart/customer-profile /-->' |
| 44 |
] |
| 45 |
]; |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
public function createPages(array $exclude = []): array |
| 50 |
{ |
| 51 |
$storeSettings = new StoreSettings(); |
| 52 |
$createdPages = []; |
| 53 |
$pages = Arr::except($this->pages(), $exclude); |
| 54 |
foreach ($pages as $key => $page) { |
| 55 |
$title = $page['title']; |
| 56 |
$pageKey = "{$key}_page_id"; |
| 57 |
|
| 58 |
$content = $page['content']; |
| 59 |
$info = [ |
| 60 |
'post_title' => $title, |
| 61 |
'post_content' => $content, |
| 62 |
'post_status' => 'publish', |
| 63 |
'post_type' => 'page', |
| 64 |
'post_name' => $title, |
| 65 |
]; |
| 66 |
$pageId = wp_insert_post($info); |
| 67 |
$storeSettings->set($pageKey, $pageId); |
| 68 |
$createdPages[$pageKey] = $pageId; |
| 69 |
|
| 70 |
|
| 71 |
} |
| 72 |
return $createdPages; |
| 73 |
} |
| 74 |
|
| 75 |
public function getGeneratablePage(bool $withDefaultId = false): array |
| 76 |
{ |
| 77 |
$pages = $this->pages(); |
| 78 |
if ($withDefaultId) { |
| 79 |
foreach ($pages as $key => $page) { |
| 80 |
$title = $page['title']; |
| 81 |
$pageKey = "{$key}_page_id"; |
| 82 |
$page = (new DynamicModel([], 'posts')) |
| 83 |
->newQuery() |
| 84 |
->where('post_type', 'page') |
| 85 |
->where('post_status', 'publish') |
| 86 |
->where('post_content', 'LIKE', "%" . Str::of($page['content'])->remove('/-->') . '%') |
| 87 |
->orderByDesc('ID') |
| 88 |
->first(); |
| 89 |
if (!empty($page)) { |
| 90 |
$pages[$key]['page_id'] = $page->ID; |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
return $pages; |
| 95 |
} |
| 96 |
|
| 97 |
public static function getPages($searchBy, $ignoreCache = false) |
| 98 |
{ |
| 99 |
$searchByKey = $searchBy ?? 'all'; |
| 100 |
|
| 101 |
if (isset(static::$cachedPages[$searchByKey]) && !$ignoreCache) { |
| 102 |
return static::$cachedPages[$searchByKey]; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
$results = (new DynamicModel([], 'posts')) |
| 107 |
->newQuery() |
| 108 |
->select('ID', 'post_title') |
| 109 |
->where('post_type', 'page') |
| 110 |
->where('post_status', 'publish') |
| 111 |
->where('post_title', 'LIKE', "%" . esc_sql($searchBy) . '%') |
| 112 |
->get(); |
| 113 |
|
| 114 |
|
| 115 |
$matchedPages = []; |
| 116 |
foreach ($results as $result) { |
| 117 |
$matchedPages[] = [ |
| 118 |
'label' => $result->post_title . "( $result->ID )", |
| 119 |
'value' => $result->ID, |
| 120 |
]; |
| 121 |
} |
| 122 |
|
| 123 |
static::$cachedPages[$searchByKey] = $matchedPages; |
| 124 |
return $matchedPages; |
| 125 |
} |
| 126 |
|
| 127 |
public function handlePageDelete() |
| 128 |
{ |
| 129 |
add_action('wp_trash_post', function ($post_id) { |
| 130 |
if (get_post_type($post_id) === 'page') { |
| 131 |
$settings = (new \FluentCart\Api\StoreSettings()); |
| 132 |
$pageSettings = $settings->getPagesSettings(); |
| 133 |
$allSettings = $settings->get(); |
| 134 |
$pageId = $post_id; |
| 135 |
|
| 136 |
foreach ($pageSettings as $pageName => $pageSetting) { |
| 137 |
if ($pageSetting == $pageId) { |
| 138 |
$allSettings[$pageName] = ''; |
| 139 |
} |
| 140 |
} |
| 141 |
$settings->save($allSettings); |
| 142 |
} |
| 143 |
}); |
| 144 |
} |
| 145 |
|
| 146 |
public static function isPage($pageId): bool |
| 147 |
{ |
| 148 |
static $cachedIsPage = []; |
| 149 |
|
| 150 |
if (isset($cachedIsPage[$pageId])) { |
| 151 |
return $cachedIsPage[$pageId]; |
| 152 |
} |
| 153 |
|
| 154 |
$post = get_post($pageId); |
| 155 |
if (empty($post) || $post->post_status !== 'publish') { |
| 156 |
$cachedIsPage[$pageId] = false; |
| 157 |
return false; |
| 158 |
} |
| 159 |
|
| 160 |
$cachedIsPage[$pageId] = $post->post_type == 'page'; |
| 161 |
|
| 162 |
return $cachedIsPage[$pageId]; |
| 163 |
} |
| 164 |
|
| 165 |
public static function getShopPageContent(): string |
| 166 |
{ |
| 167 |
|
| 168 |
$fluentCartProductsBlock = "<!-- wp:fluent-cart/products {\"colors\":{},\"enable_filter\":true,\"enable_wildcard_filter\":true,\"filters\":{\"product-categories\":{\"filter_type\":\"options\",\"is_meta\":true,\"label\":\"Product Categories\",\"enabled\":true,\"multiple\":false},\"product-brands\":{\"filter_type\":\"options\",\"is_meta\":true,\"label\":\"Product Brands\",\"enabled\":true,\"multiple\":false},\"price_range\":{\"filter_type\":\"range\",\"is_meta\":false,\"label\":\"Price\",\"enabled\":true}}} --> |
| 169 |
<div class=\"wp-block-fluent-cart-products\"><div class=\"fct-products-wrapper\" data-fluent-cart-shop-app=\"true\" data-fluent-cart-product-wrapper=\"\"><!-- wp:fluent-cart/shopapp-product-view-switcher {\"className\":\"fluent-product-view-switcher\",\"metadata\":{\"name\":\"View Switcher\"}} /--> |
| 170 |
|
| 171 |
<!-- wp:fluent-cart/shopapp-product-container {\"className\":\"fluent-product-container\"} --> |
| 172 |
<div class=\"wp-block-fluent-cart-shopapp-product-container fluent-product-container\"><!-- wp:fluent-cart/shopapp-product-filter --> |
| 173 |
<div class=\"fluent-cart-product-filter-wrapper\"><!-- wp:fluent-cart/shopapp-product-filter-search-box /--> |
| 174 |
|
| 175 |
<!-- wp:fluent-cart/shopapp-product-filter-filters /--> |
| 176 |
|
| 177 |
<!-- wp:fluent-cart/shopapp-product-filter-button --> |
| 178 |
<div class=\"fct-product-block-filter-item\"><!-- wp:fluent-cart/shopapp-product-filter-apply-button /--> |
| 179 |
|
| 180 |
<!-- wp:fluent-cart/shopapp-product-filter-reset-button /--></div> |
| 181 |
<!-- /wp:fluent-cart/shopapp-product-filter-button --></div> |
| 182 |
<!-- /wp:fluent-cart/shopapp-product-filter --> |
| 183 |
|
| 184 |
<!-- wp:fluent-cart/shopapp-product-loop {\"wp_client_id\":\"8c5b4ab5-1e93-4e68-867c-34ecd6250211\",\"last_changed\":\"2025-10-03T11:23:14.858Z\",\"className\":\"fluent-product-loop\",\"metadata\":{\"name\":\"Product Loop\"}} --> |
| 185 |
<div class=\"fluent-cart-product-loop fct-product-block-editor-product-card\"><!-- wp:fluent-cart/shopapp-product-image --> |
| 186 |
<div class=\"fluent-cart-product-image\"></div> |
| 187 |
<!-- /wp:fluent-cart/shopapp-product-image --> |
| 188 |
|
| 189 |
<!-- wp:fluent-cart/shopapp-product-title /--> |
| 190 |
|
| 191 |
<!-- wp:fluent-cart/shopapp-product-price /--> |
| 192 |
|
| 193 |
<!-- wp:fluent-cart/shopapp-product-buttons /--></div> |
| 194 |
<!-- /wp:fluent-cart/shopapp-product-loop --></div> |
| 195 |
<!-- /wp:fluent-cart/shopapp-product-container --> |
| 196 |
|
| 197 |
<!-- wp:fluent-cart/product-paginator {\"className\":\"fluent-product-paginator\",\"metadata\":{\"name\":\"Paginator\"}} --> |
| 198 |
<div class=\"fluent-cart-product-paginator\"><!-- wp:fluent-cart/product-paginator-info /--> |
| 199 |
|
| 200 |
<!-- wp:fluent-cart/product-paginator-number /--></div> |
| 201 |
<!-- /wp:fluent-cart/product-paginator --> |
| 202 |
|
| 203 |
<!-- wp:fluent-cart/shopapp-product-no-result {\"className\":\"fluent-product-no-result\",\"metadata\":{\"name\":\"No Result\"}} --> |
| 204 |
<div class=\"fct-product-block-filter-item\"><!-- wp:paragraph {\"align\":\"center\",\"fontSize\":\"large\"} --> |
| 205 |
<p class=\"has-text-align-center has-large-font-size\">No results found</p> |
| 206 |
<!-- /wp:paragraph --> |
| 207 |
|
| 208 |
<!-- wp:paragraph {\"align\":\"center\"} --> |
| 209 |
<p class=\"has-text-align-center\">You can try <a href=\"#\">clearing any filters</a> or head to our <a href=\"#\">store's home</a></p> |
| 210 |
<!-- /wp:paragraph --></div> |
| 211 |
<!-- /wp:fluent-cart/shopapp-product-no-result --></div></div> |
| 212 |
<!-- /wp:fluent-cart/products -->"; |
| 213 |
|
| 214 |
// Example usage |
| 215 |
return $fluentCartProductsBlock; |
| 216 |
|
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
} |
| 221 |
|