PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 2.10.01
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v2.10.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 / Paywalls.php

Paywalls.php in FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses 2.10.01, at Modules/Integrations/FluentCart/Paywalls.php

184 lines 6.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;
4
5 use FluentCart\App\Models\ProductMeta;
6 use FluentCommunity\Framework\Support\Arr;
7
8 class Paywalls
9 {
10 public function register($app)
11 {
12 $app->router->group(function ($router) {
13 require_once __DIR__ . '/Http/cart_api.php';
14 });
15
16 add_filter('fluent_community/portal_vars', [$this, 'addPortalVars'], 10, 1);
17 add_action('fluent_community/paywall_added', [$this, 'maybeCreateIntegration'], 10, 2);
18 add_action('fluent_community/paywall_removed', [$this, 'maybeRemoveIntegration'], 10, 3);
19 add_filter('fluent_community/lockscreen_fields', [$this, 'maybeAddPaywallField'], 10, 2);
20 add_filter('fluent_community/lockscreen_formatted_field', [$this, 'maybeFormatPaywallField'], 10, 2);
21 }
22
23 public function addPortalVars($vars)
24 {
25 $vars['features']['has_fluentcart'] = true;
26 return $vars;
27 }
28
29 public function maybeCreateIntegration($space, $productId)
30 {
31 $communityIntegrations = ProductMeta::query()
32 ->where('object_id', $productId)
33 ->where('object_type', 'product_integration')
34 ->where('meta_key', 'fluent_community')
35 ->get();
36
37 $checkIds = $space->type == 'course' ? 'course_ids' : 'space_ids';
38
39 foreach ($communityIntegrations as $integration) {
40 /** @var \FluentCart\App\Models\ProductMeta $integration */
41 $data = $integration->meta_value;
42 $isEnabled = Arr::get($data, 'enabled', 'no');
43 $eventTrigger = Arr::get($data, 'event_trigger');
44 if ($isEnabled != 'yes' || !$eventTrigger || !in_array('order_paid_done', $eventTrigger)) {
45 continue;
46 }
47
48 $existingIds = Arr::get($data, $checkIds, []);
49 if (in_array($space->id, $existingIds)) {
50 return;
51 }
52
53 $data[$checkIds] = array_merge($existingIds, [(string)$space->id]);
54 $integration->meta_value = $data;
55 $integration->save();
56
57 do_action('fluent_community/product_integration_feed_updated', $integration->id, $space->id);
58
59 return;
60 }
61
62 $integrationFeedData = [
63 'enabled' => 'yes',
64 'name' => 'FluentCommunity Integration Feed',
65 'space_ids' => [],
66 'remove_space_ids' => [],
67 'course_ids' => [],
68 'remove_course_ids' => [],
69 'event_trigger' => ['order_paid_done'],
70 'tag_ids_selection_type' => 'simple',
71 'mark_as_verified' => 'no',
72 'watch_on_access_revoke' => 'yes'
73 ];
74
75 $integrationFeedData[$checkIds] = [(string) $space->id];
76
77 $communityIntegration = ProductMeta::create([
78 'object_id' => $productId,
79 'object_type' => 'product_integration',
80 'meta_key' => 'fluent_community', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
81 'meta_value' => $integrationFeedData // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
82 ]);
83
84 do_action('fluent_community/product_integration_feed_created', $communityIntegration->id ?? null, $productId);
85 }
86
87 public function maybeRemoveIntegration($space, $productId, $requestData)
88 {
89 $revokeAccess = Arr::get($requestData, 'revoke_access', 'no');
90 if ($revokeAccess !== 'yes') {
91 return;
92 }
93
94 $communityIntegrations = ProductMeta::query()
95 ->where('object_id', $productId)
96 ->where('object_type', 'product_integration')
97 ->where('meta_key', 'fluent_community')
98 ->get();
99
100 $checkIds = $space->type == 'course' ? 'course_ids' : 'space_ids';
101
102 foreach ($communityIntegrations as $integration) {
103 /** @var \FluentCart\App\Models\ProductMeta $integration */
104 $data = $integration->meta_value;
105 $isEnabled = Arr::get($data, 'enabled', 'no');
106 $eventTrigger = Arr::get($data, 'event_trigger');
107 if ($isEnabled != 'yes' || !$eventTrigger || !in_array('order_paid_done', $eventTrigger)) {
108 continue;
109 }
110
111 $existingIds = Arr::get($data, $checkIds, []);
112 if (!in_array($space->id, $existingIds)) {
113 continue;
114 }
115
116 $data[$checkIds] = array_diff($existingIds, [(string)$space->id]);
117 $integration->meta_value = $data;
118 $integration->save();
119
120 do_action('fluent_community/product_integration_feed_updated', $integration->id, $space->id);
121
122 return;
123 }
124 }
125
126 public function maybeAddPaywallField($fields, $space)
127 {
128 $paywallField = [
129 'hidden' => true,
130 'type' => 'paywall',
131 'label' => __('Paywalls', 'fluent-community'),
132 'name' => 'paywall',
133 'paywalls' => [],
134 'text_color' => '#FFFFFF',
135 'button_text' => __('Join', 'fluent-community'),
136 'button_color' => '#2B2E33',
137 'background_color' => '',
138 'button_text_color' => '#FFFFFF',
139 'show_description' => 'no'
140 ];
141
142 $existingNames = array_column($fields, 'name');
143
144 if ($space->hasPaywallIntegration()) {
145 if (!in_array('paywall', $existingNames)) {
146 $actionIndex = array_search('action', $existingNames);
147 if ($actionIndex !== false) {
148 array_splice($fields, $actionIndex, 0, [$paywallField]);
149 } else {
150 $fields[] = $paywallField;
151 }
152 }
153 } else {
154 if (in_array('paywall', $existingNames)) {
155 $fields = array_values(array_filter($fields, function($field) {
156 return $field['name'] != 'paywall';
157 }));
158 }
159 }
160
161 return $fields;
162 }
163
164 public function maybeFormatPaywallField($field, $value)
165 {
166 if (Arr::get($value, 'type') != 'paywall') {
167 return $field;
168 }
169
170 $field['paywalls'] = array_values(array_filter(
171 array_map(function($paywall) {
172 return sanitize_text_field($paywall);
173 }, $value['paywalls'] ?? []),
174 function($paywall) {
175 return !empty($paywall) && is_numeric($paywall);
176 }
177 ));
178
179 $field['show_description'] = Arr::get($value, 'show_description') == 'yes' ? 'yes' : 'no';
180
181 return $field;
182 }
183 }
184