PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 2.10.0
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v2.10.0
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.10.0, at Modules/Integrations/FluentCart/Http/Controllers/PaywallController.php

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