PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.7.0
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.7.0
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 1.3.20 All 50 releases
fluent-cart / app / CPT / Pages.php

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

225 lines 8.4 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\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 'order_review' => [
38 'title' => 'Review Your Order',
39 'content' => '[fluent_cart_order_review]'
40 ],
41 'shop' => [
42 'title' => 'Shop',
43 'content' => static::getShopPageContent()
44 ],
45 'customer_profile' => [
46 'title' => 'Account',
47 'content' => '<!-- wp:fluent-cart/customer-profile /-->'
48 ]
49 ];
50 }
51
52
53 public function createPages(array $exclude = []): array
54 {
55 $storeSettings = new StoreSettings();
56 $createdPages = [];
57 $pages = Arr::except($this->pages(), $exclude);
58 foreach ($pages as $key => $page) {
59 $title = $page['title'];
60 $pageKey = "{$key}_page_id";
61
62 $content = $page['content'];
63 $info = [
64 'post_title' => $title,
65 'post_content' => $content,
66 'post_status' => 'publish',
67 'post_type' => 'page',
68 'post_name' => $title,
69 ];
70 $pageId = wp_insert_post($info);
71 $storeSettings->set($pageKey, $pageId);
72 $createdPages[$pageKey] = $pageId;
73
74
75 }
76 return $createdPages;
77 }
78
79 public function getGeneratablePage(bool $withDefaultId = false): array
80 {
81 $pages = $this->pages();
82 if ($withDefaultId) {
83 foreach ($pages as $key => $page) {
84 $title = $page['title'];
85 $pageKey = "{$key}_page_id";
86 $page = (new DynamicModel([], 'posts'))
87 ->newQuery()
88 ->where('post_type', 'page')
89 ->where('post_status', 'publish')
90 ->where('post_content', 'LIKE', "%" . Str::of($page['content'])->remove('/-->') . '%')
91 ->orderByDesc('ID')
92 ->first();
93 if (!empty($page)) {
94 $pages[$key]['page_id'] = $page->ID;
95 }
96 }
97 }
98 return $pages;
99 }
100
101 public static function getPages($searchBy, $ignoreCache = false)
102 {
103 $searchByKey = $searchBy ?? 'all';
104
105 if (isset(static::$cachedPages[$searchByKey]) && !$ignoreCache) {
106 return static::$cachedPages[$searchByKey];
107 }
108
109
110 $results = (new DynamicModel([], 'posts'))
111 ->newQuery()
112 ->select('ID', 'post_title')
113 ->where('post_type', 'page')
114 ->where('post_status', 'publish')
115 ->where('post_title', 'LIKE', "%" . esc_sql($searchBy) . '%')
116 ->get();
117
118
119 $matchedPages = [];
120 foreach ($results as $result) {
121 $matchedPages[] = [
122 'label' => $result->post_title . "( $result->ID )",
123 'value' => $result->ID,
124 ];
125 }
126
127 static::$cachedPages[$searchByKey] = $matchedPages;
128 return $matchedPages;
129 }
130
131 public function handlePageDelete()
132 {
133 add_action('wp_trash_post', function ($post_id) {
134 if (get_post_type($post_id) === 'page') {
135 $settings = (new \FluentCart\Api\StoreSettings());
136 $pageSettings = $settings->getPagesSettings();
137 $allSettings = $settings->get();
138 $pageId = $post_id;
139
140 foreach ($pageSettings as $pageName => $pageSetting) {
141 if ($pageSetting == $pageId) {
142 $allSettings[$pageName] = '';
143 }
144 }
145 $settings->save($allSettings);
146 }
147 });
148 }
149
150 public static function isPage($pageId): bool
151 {
152 static $cachedIsPage = [];
153
154 if (isset($cachedIsPage[$pageId])) {
155 return $cachedIsPage[$pageId];
156 }
157
158 $post = get_post($pageId);
159 if (empty($post) || $post->post_status !== 'publish') {
160 $cachedIsPage[$pageId] = false;
161 return false;
162 }
163
164 $cachedIsPage[$pageId] = $post->post_type == 'page';
165
166 return $cachedIsPage[$pageId];
167 }
168
169 public static function getShopPageContent(): string
170 {
171
172 $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}}} -->
173 <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\"}} /-->
174
175 <!-- wp:fluent-cart/shopapp-product-container {\"className\":\"fluent-product-container\"} -->
176 <div class=\"wp-block-fluent-cart-shopapp-product-container fluent-product-container\"><!-- wp:fluent-cart/shopapp-product-filter -->
177 <div class=\"fluent-cart-product-filter-wrapper\"><!-- wp:fluent-cart/shopapp-product-filter-search-box /-->
178
179 <!-- wp:fluent-cart/shopapp-product-filter-filters /-->
180
181 <!-- wp:fluent-cart/shopapp-product-filter-button -->
182 <div class=\"fct-product-block-filter-item\"><!-- wp:fluent-cart/shopapp-product-filter-apply-button /-->
183
184 <!-- wp:fluent-cart/shopapp-product-filter-reset-button /--></div>
185 <!-- /wp:fluent-cart/shopapp-product-filter-button --></div>
186 <!-- /wp:fluent-cart/shopapp-product-filter -->
187
188 <!-- 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\"}} -->
189 <div class=\"fluent-cart-product-loop fct-product-block-editor-product-card\"><!-- wp:fluent-cart/shopapp-product-image -->
190 <div class=\"fluent-cart-product-image\"></div>
191 <!-- /wp:fluent-cart/shopapp-product-image -->
192
193 <!-- wp:fluent-cart/shopapp-product-title /-->
194
195 <!-- wp:fluent-cart/shopapp-product-price /-->
196
197 <!-- wp:fluent-cart/shopapp-product-buttons /--></div>
198 <!-- /wp:fluent-cart/shopapp-product-loop --></div>
199 <!-- /wp:fluent-cart/shopapp-product-container -->
200
201 <!-- wp:fluent-cart/product-paginator {\"className\":\"fluent-product-paginator\",\"metadata\":{\"name\":\"Paginator\"}} -->
202 <div class=\"fluent-cart-product-paginator\"><!-- wp:fluent-cart/product-paginator-info /-->
203
204 <!-- wp:fluent-cart/product-paginator-number /--></div>
205 <!-- /wp:fluent-cart/product-paginator -->
206
207 <!-- wp:fluent-cart/shopapp-product-no-result {\"className\":\"fluent-product-no-result\",\"metadata\":{\"name\":\"No Result\"}} -->
208 <div class=\"fct-product-block-filter-item\"><!-- wp:paragraph {\"align\":\"center\",\"fontSize\":\"large\"} -->
209 <p class=\"has-text-align-center has-large-font-size\">No results found</p>
210 <!-- /wp:paragraph -->
211
212 <!-- wp:paragraph {\"align\":\"center\"} -->
213 <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>
214 <!-- /wp:paragraph --></div>
215 <!-- /wp:fluent-cart/shopapp-product-no-result --></div></div>
216 <!-- /wp:fluent-cart/products -->";
217
218 // Example usage
219 return $fluentCartProductsBlock;
220
221 }
222
223
224 }
225