PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 2.6.01
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v2.6.01
2.10.0 2.10.01 2.9.1 2.9.0 2.8.1 2.8.0 2.7.7 2.7.5 2.7.0 2.6.01 2.6.0 2.5.0 2.4.01 trunk 1.0.90 1.0.91 1.0.92 1.0.93 1.0.94 1.0.95 1.0.96 1.0.97 1.0.98 1.0.99 1.1.0 All 77 releases
fluent-community / Modules / Integrations / FluentCart / Http / Controllers / PaywallController.php

PaywallController.php in FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses 2.6.01, at Modules/Integrations/FluentCart/Http/Controllers/PaywallController.php

268 lines 9.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCommunity\Modules\Integrations\FluentCart\Http\Controllers;
4
5 use FluentCart\App\Models\Product;
6 use FluentCart\App\Models\ProductDetail;
7 use FluentCart\App\Models\ProductVariation;
8 use FluentCart\App\Services\Permission\PermissionManager;
9 use FluentCommunity\App\Models\BaseSpace;
10 use FluentCommunity\Framework\Support\Arr;
11 use FluentCommunity\App\Services\Helper;
12 use FluentCart\App\Helpers\Helper as CartHelper;
13 use FluentCommunity\Framework\Http\Request\Request;
14 use FluentCommunity\App\Http\Controllers\Controller;
15
16 class PaywallController extends Controller
17 {
18 public function getPaywalls(Request $request, $spaceId)
19 {
20 $space = BaseSpace::withoutGlobalScopes()->findOrFail($spaceId);
21
22 $paywallIds = array_map('intval', $request->get('paywall_ids', []));
23 $productIds = Arr::get($space->settings, 'cart_product_ids', []);
24
25 $paywallQuery = Product::whereIn('ID', $productIds)
26 ->select('ID', 'post_title', 'post_status', 'post_excerpt')
27 ->where('post_status', 'publish')
28 ->with(['detail', 'variants' => function ($query) {
29 $query->select('id', 'post_id', 'item_price', 'variation_title', 'other_info');
30 }]);
31
32 if (!empty($paywallIds)) {
33 $paywallQuery->whereHas('variants', function ($query) use ($paywallIds) {
34 $query->whereIn('id', (array) $paywallIds);
35 });
36 }
37
38 $paywalls = $paywallQuery->get();
39
40 $baseUrl = Helper::baseUrl('/');
41 $cartAdminUrl = apply_filters('fluent_cart/admin_base_url', admin_url('admin.php?page=fluent-cart#/'), []);
42
43 $paywalls = $paywalls->map(function ($paywall) use ($baseUrl, $cartAdminUrl, $spaceId) {
44 $paywall->view_url = $paywall->view_url;
45 $paywall->admin_url = $cartAdminUrl . 'products/' . $paywall->ID;
46 $isSimple = Arr::get($paywall->detail, 'variation_type') == 'simple';
47 $paywall->variants->map(function ($variant) use ($baseUrl, $spaceId, $isSimple, $paywall) {
48 $variant->photo = $isSimple ? $paywall->thumbnail : $variant->thumbnail;
49 $variant->total_price = $variant->formatted_total;
50 $variant->checkout_url = add_query_arg([
51 'fluent-cart' => 'instant_checkout',
52 'item_id' => $variant->id,
53 'quantity' => 1,
54 'redirect_to' => $baseUrl . 'checkout?space_id=' . $spaceId
55 ], site_url());
56 return $variant;
57 });
58 return $paywall;
59 });
60
61 return [
62 'paywalls' => $paywalls
63 ];
64 }
65
66 public function addPaywall(Request $request, $spaceId)
67 {
68 $space = BaseSpace::withoutGlobalScopes()->findOrFail($spaceId);
69
70 $productId = $request->getSafe('cart_product_id', 'intval');
71
72 $product = Product::where('post_status', 'publish')->where('ID', $productId)->first();
73 if (!$product) {
74 return $this->sendError([
75 'message' => __('Product not found', 'fluent-community')
76 ]);
77 }
78
79 $spaceSettings = $space->settings;
80 $cartProductIds = Arr::get($spaceSettings, 'cart_product_ids', []);
81
82 if (in_array($productId, $cartProductIds)) {
83 return $this->sendError([
84 'message' => __('Product already exists', 'fluent-community')
85 ]);
86 }
87
88 $cartProductIds[] = $productId;
89 $spaceSettings['cart_product_ids'] = array_values($cartProductIds);
90 $space->settings = $spaceSettings;
91 $space->save();
92
93 do_action('fluent_community/paywall_added', $space, $productId);
94
95 return [
96 'message' => __('Paywall has been added', 'fluent-community'),
97 'paywall' => $product
98 ];
99 }
100
101 public function removePaywall(Request $request, $spaceId)
102 {
103 $space = BaseSpace::withoutGlobalScopes()->findOrFail($spaceId);
104
105 $productId = $request->getSafe('cart_product_id', 'intval');
106
107 $product = Product::where('post_status', 'publish')->where('ID', $productId)->first();
108 if (!$product) {
109 return $this->sendError([
110 'message' => __('Product not found', 'fluent-community')
111 ]);
112 }
113
114 $spaceSettings = $space->settings;
115 $cartProductIds = Arr::get($spaceSettings, 'cart_product_ids', []);
116
117 if (!in_array($productId, $cartProductIds)) {
118 return $this->sendError([
119 'message' => __('Product not added to this community', 'fluent-community')
120 ]);
121 }
122
123 $cartProductIds = array_diff($cartProductIds, [$productId]);
124 $spaceSettings['cart_product_ids'] = array_values($cartProductIds);
125 $space->settings = $spaceSettings;
126 $space->save();
127
128 do_action('fluent_community/paywall_removed', $space, $productId, $request->all());
129
130 return [
131 'message' => __('Paywall has been removed', 'fluent-community'),
132 ];
133 }
134
135 public function searchProduct(Request $request)
136 {
137 $search = trim($request->getSafe('search'));
138
139 $products = Product::query()
140 ->select('ID', 'post_title')
141 ->when($search !== '', function ($query) use ($search) {
142 $query->where('post_title', 'LIKE', '%' . $search . '%');
143 })
144 ->limit(20)
145 ->where('post_status', 'publish')
146 ->whereHas('detail')
147 ->whereHas('variants')
148 ->with(['variants' => function ($query) {
149 $query->select('id', 'post_id', 'item_price');
150 }])
151 ->get();
152
153 $formattedProducts = $products->map(function ($product) {
154 return [
155 'id' => $product->ID,
156 'title' => $product->post_title,
157 'image' => $product->thumbnail,
158 'price' => $this->getFormattedPrice($product)
159 ];
160 });
161
162 return [
163 'products' => $formattedProducts
164 ];
165 }
166
167 public function createProduct(Request $request)
168 {
169 if (!PermissionManager::hasPermission('products/create')) {
170 return $this->sendError([
171 'message' => __('You do not have permission to create products', 'fluent-community')
172 ], 422);
173 }
174
175 $title = $request->getSafe('title');
176 $price = $request->getSafe('price', 'floatval');
177
178 $postData = [
179 'post_title' => $title,
180 'post_name' => sanitize_title($title),
181 'post_status' => 'publish',
182 'post_type' => \FluentCart\App\CPT\FluentProducts::CPT_NAME,
183 ];
184
185 $createdPostId = wp_insert_post($postData);
186
187 if (is_wp_error($createdPostId)) {
188 return $this->sendError([
189 'code' => 403,
190 'message' => $createdPostId->get_error_message()
191 ]);
192 }
193
194 $detailData = [
195 'post_id' => $createdPostId,
196 'fulfillment_type' => 'digital',
197 'min_price' => $price * 100,
198 'max_price' => $price * 100,
199 ];
200
201 $productDetail = ProductDetail::create($detailData);
202
203 $variationData = [
204 'post_id' => $createdPostId,
205 'serial_index' => 1,
206 'variation_title' => $title,
207 'stock_status' => 'in-stock',
208 'payment_type' => 'onetime',
209 'total_stock' => 1,
210 'available' => 1,
211 'fulfillment_type' => 'digital',
212 'item_price' => $price * 100,
213 'other_info' => [
214 'description' => '',
215 'payment_type' => 'onetime',
216 'times' => '',
217 'repeat_interval' => '',
218 'trial_days' => '',
219 'billing_summary' => '',
220 'manage_setup_fee' => 'no',
221 'signup_fee_name' => '',
222 'signup_fee' => '',
223 'setup_fee_per_item' => 'no',
224 ]
225 ];
226
227 $variation = ProductVariation::create($variationData);
228
229 if (!$productDetail || !$variation) {
230 return $this->sendError([
231 'code' => 403,
232 'message' => __('Failed to create product', 'fluent-community')
233 ]);
234 }
235
236 $product = Product::where('ID', $createdPostId)->first();
237
238 $product->updateProductMeta('created_from', 'fluent_community');
239
240 $productData = [
241 'id' => $product->ID,
242 'title' => $product->post_title,
243 'image' => $product->thumbnail,
244 'price' => $this->getFormattedPrice($product)
245 ];
246
247 return [
248 'product' => $productData
249 ];
250 }
251
252 private function getFormattedPrice($product)
253 {
254 if (!$product->detail) {
255 return '';
256 }
257
258 $minPrice = $product->detail->min_price;
259 $maxPrice = $product->detail->max_price;
260
261 $formattedPrice = ($minPrice === $maxPrice)
262 ? CartHelper::toDecimal($minPrice)
263 : CartHelper::toDecimal($minPrice) . ' - ' . CartHelper::toDecimal($maxPrice);
264
265 return $formattedPrice;
266 }
267 }
268