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

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

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