PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.7.1
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.7.1
1.7.1 1.7.0 1.6.6 1.6.5 1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 All 51 releases
fluent-cart / app / CPT / Pages.php

Pages.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.7.1, at app/CPT/Pages.php

237 lines 8.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCart\App\CPT;
4
5 use FluentCart\Api\ModuleSettings;
6 use FluentCart\Api\StoreSettings;
7 use FluentCart\App\Models\DynamicModel;
8 use FluentCart\Framework\Support\Arr;
9 use FluentCart\Framework\Support\Str;
10
11 class Pages
12 {
13
14 protected static array $cachedPages = [];
15
16 public function pages(): array
17 {
18 // Lets addons register their own generatable pages ('key' => title + content),
19 // so the Pages Setup "+" create button and onboarding can build them.
20 return apply_filters('fluent_cart/generatable_pages', $this->corePages());
21 }
22
23 public function corePages(): array
24 {
25 $pages = [
26 'checkout' => [
27 'title' => 'Checkout',
28 'content' => '[fluent_cart_checkout]'
29 ],
30 'cart' => [
31 'title' => 'Cart',
32 'content' => '[fluent_cart_cart]'
33 ],
34 'receipt' => [
35 'title' => 'Receipt',
36 'content' => '[fluent_cart_receipt]'
37 ],
38 'order_review' => [
39 'title' => 'Review Your Order',
40 'content' => '[fluent_cart_order_review]'
41 ],
42 'shop' => [
43 'title' => 'Shop',
44 'content' => static::getShopPageContent()
45 ],
46 'customer_profile' => [
47 'title' => 'Account',
48 'content' => '<!-- wp:fluent-cart/customer-profile /-->'
49 ]
50 ];
51
52 // The order review page serves the reviews module. While the module
53 // is off it is not a page the store is missing: the dashboard must
54 // not ask for it, onboarding must not create it, and Pages Setup
55 // does not offer it (StoreSettings::fields()). Read per call, so
56 // switching the module on makes it wanted again at once.
57 if (!ModuleSettings::isActive('reviews')) {
58 unset($pages['order_review']);
59 }
60
61 return $pages;
62 }
63
64
65 public function createPages(array $exclude = []): array
66 {
67 $storeSettings = new StoreSettings();
68 $createdPages = [];
69 $pages = Arr::except($this->pages(), $exclude);
70 foreach ($pages as $key => $page) {
71 $title = $page['title'];
72 $pageKey = "{$key}_page_id";
73
74 $content = $page['content'];
75 $info = [
76 'post_title' => $title,
77 'post_content' => $content,
78 'post_status' => 'publish',
79 'post_type' => 'page',
80 'post_name' => $title,
81 ];
82 $pageId = wp_insert_post($info);
83 $storeSettings->set($pageKey, $pageId);
84 $createdPages[$pageKey] = $pageId;
85
86
87 }
88 return $createdPages;
89 }
90
91 public function getGeneratablePage(bool $withDefaultId = false): array
92 {
93 $pages = $this->pages();
94 if ($withDefaultId) {
95 foreach ($pages as $key => $page) {
96 $title = $page['title'];
97 $pageKey = "{$key}_page_id";
98 $page = (new DynamicModel([], 'posts'))
99 ->newQuery()
100 ->where('post_type', 'page')
101 ->where('post_status', 'publish')
102 ->where('post_content', 'LIKE', "%" . Str::of($page['content'])->remove('/-->') . '%')
103 ->orderByDesc('ID')
104 ->first();
105 if (!empty($page)) {
106 $pages[$key]['page_id'] = $page->ID;
107 }
108 }
109 }
110 return $pages;
111 }
112
113 public static function getPages($searchBy, $ignoreCache = false)
114 {
115 $searchByKey = $searchBy ?? 'all';
116
117 if (isset(static::$cachedPages[$searchByKey]) && !$ignoreCache) {
118 return static::$cachedPages[$searchByKey];
119 }
120
121
122 $results = (new DynamicModel([], 'posts'))
123 ->newQuery()
124 ->select('ID', 'post_title')
125 ->where('post_type', 'page')
126 ->where('post_status', 'publish')
127 ->where('post_title', 'LIKE', "%" . esc_sql($searchBy) . '%')
128 ->get();
129
130
131 $matchedPages = [];
132 foreach ($results as $result) {
133 $matchedPages[] = [
134 'label' => $result->post_title . "( $result->ID )",
135 'value' => $result->ID,
136 ];
137 }
138
139 static::$cachedPages[$searchByKey] = $matchedPages;
140 return $matchedPages;
141 }
142
143 public function handlePageDelete()
144 {
145 add_action('wp_trash_post', function ($post_id) {
146 if (get_post_type($post_id) === 'page') {
147 $settings = (new \FluentCart\Api\StoreSettings());
148 $pageSettings = $settings->getPagesSettings();
149 $allSettings = $settings->get();
150 $pageId = $post_id;
151
152 foreach ($pageSettings as $pageName => $pageSetting) {
153 if ($pageSetting == $pageId) {
154 $allSettings[$pageName] = '';
155 }
156 }
157 $settings->save($allSettings);
158 }
159 });
160 }
161
162 public static function isPage($pageId): bool
163 {
164 static $cachedIsPage = [];
165
166 if (isset($cachedIsPage[$pageId])) {
167 return $cachedIsPage[$pageId];
168 }
169
170 $post = get_post($pageId);
171 if (empty($post) || $post->post_status !== 'publish') {
172 $cachedIsPage[$pageId] = false;
173 return false;
174 }
175
176 $cachedIsPage[$pageId] = $post->post_type == 'page';
177
178 return $cachedIsPage[$pageId];
179 }
180
181 public static function getShopPageContent(): string
182 {
183
184 $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}}} -->
185 <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\"}} /-->
186
187 <!-- wp:fluent-cart/shopapp-product-container {\"className\":\"fluent-product-container\"} -->
188 <div class=\"wp-block-fluent-cart-shopapp-product-container fluent-product-container\"><!-- wp:fluent-cart/shopapp-product-filter -->
189 <div class=\"fluent-cart-product-filter-wrapper\"><!-- wp:fluent-cart/shopapp-product-filter-search-box /-->
190
191 <!-- wp:fluent-cart/shopapp-product-filter-filters /-->
192
193 <!-- wp:fluent-cart/shopapp-product-filter-button -->
194 <div class=\"fct-product-block-filter-item\"><!-- wp:fluent-cart/shopapp-product-filter-apply-button /-->
195
196 <!-- wp:fluent-cart/shopapp-product-filter-reset-button /--></div>
197 <!-- /wp:fluent-cart/shopapp-product-filter-button --></div>
198 <!-- /wp:fluent-cart/shopapp-product-filter -->
199
200 <!-- 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\"}} -->
201 <div class=\"fluent-cart-product-loop fct-product-block-editor-product-card\"><!-- wp:fluent-cart/shopapp-product-image -->
202 <div class=\"fluent-cart-product-image\"></div>
203 <!-- /wp:fluent-cart/shopapp-product-image -->
204
205 <!-- wp:fluent-cart/shopapp-product-title /-->
206
207 <!-- wp:fluent-cart/shopapp-product-price /-->
208
209 <!-- wp:fluent-cart/shopapp-product-buttons /--></div>
210 <!-- /wp:fluent-cart/shopapp-product-loop --></div>
211 <!-- /wp:fluent-cart/shopapp-product-container -->
212
213 <!-- wp:fluent-cart/product-paginator {\"className\":\"fluent-product-paginator\",\"metadata\":{\"name\":\"Paginator\"}} -->
214 <div class=\"fluent-cart-product-paginator\"><!-- wp:fluent-cart/product-paginator-info /-->
215
216 <!-- wp:fluent-cart/product-paginator-number /--></div>
217 <!-- /wp:fluent-cart/product-paginator -->
218
219 <!-- wp:fluent-cart/shopapp-product-no-result {\"className\":\"fluent-product-no-result\",\"metadata\":{\"name\":\"No Result\"}} -->
220 <div class=\"fct-product-block-filter-item\"><!-- wp:paragraph {\"align\":\"center\",\"fontSize\":\"large\"} -->
221 <p class=\"has-text-align-center has-large-font-size\">No results found</p>
222 <!-- /wp:paragraph -->
223
224 <!-- wp:paragraph {\"align\":\"center\"} -->
225 <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>
226 <!-- /wp:paragraph --></div>
227 <!-- /wp:fluent-cart/shopapp-product-no-result --></div></div>
228 <!-- /wp:fluent-cart/products -->";
229
230 // Example usage
231 return $fluentCartProductsBlock;
232
233 }
234
235
236 }
237