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

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

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