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

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

182 lines 6.5 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 $data = $integration->meta_value;
41 $isEnabled = Arr::get($data, 'enabled', 'no');
42 $eventTrigger = Arr::get($data, 'event_trigger');
43 if ($isEnabled != 'yes' || !$eventTrigger || !in_array('order_paid_done', $eventTrigger)) {
44 continue;
45 }
46
47 $existingIds = Arr::get($data, $checkIds, []);
48 if (in_array($space->id, $existingIds)) {
49 return;
50 }
51
52 $data[$checkIds] = array_merge($existingIds, [(string)$space->id]);
53 $integration->meta_value = $data;
54 $integration->save();
55
56 do_action('fluent_community/product_integration_feed_updated', $integration->id, $space->id);
57
58 return;
59 }
60
61 $integrationFeedData = [
62 'enabled' => 'yes',
63 'name' => 'FluentCommunity Integration Feed',
64 'space_ids' => [],
65 'remove_space_ids' => [],
66 'course_ids' => [],
67 'remove_course_ids' => [],
68 'event_trigger' => ['order_paid_done'],
69 'tag_ids_selection_type' => 'simple',
70 'mark_as_verified' => 'no',
71 'watch_on_access_revoke' => 'yes'
72 ];
73
74 $integrationFeedData[$checkIds] = [(string) $space->id];
75
76 $communityIntegration = ProductMeta::create([
77 'object_id' => $productId,
78 'object_type' => 'product_integration',
79 'meta_key' => 'fluent_community', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
80 'meta_value' => $integrationFeedData // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
81 ]);
82
83 do_action('fluent_community/product_integration_feed_created', $communityIntegration->id ?? null, $productId);
84 }
85
86 public function maybeRemoveIntegration($space, $productId, $requestData)
87 {
88 $revokeAccess = Arr::get($requestData, 'revoke_access', 'no');
89 if ($revokeAccess !== 'yes') {
90 return;
91 }
92
93 $communityIntegrations = ProductMeta::query()
94 ->where('object_id', $productId)
95 ->where('object_type', 'product_integration')
96 ->where('meta_key', 'fluent_community')
97 ->get();
98
99 $checkIds = $space->type == 'course' ? 'course_ids' : 'space_ids';
100
101 foreach ($communityIntegrations as $integration) {
102 $data = $integration->meta_value;
103 $isEnabled = Arr::get($data, 'enabled', 'no');
104 $eventTrigger = Arr::get($data, 'event_trigger');
105 if ($isEnabled != 'yes' || !$eventTrigger || !in_array('order_paid_done', $eventTrigger)) {
106 continue;
107 }
108
109 $existingIds = Arr::get($data, $checkIds, []);
110 if (!in_array($space->id, $existingIds)) {
111 continue;
112 }
113
114 $data[$checkIds] = array_diff($existingIds, [(string)$space->id]);
115 $integration->meta_value = $data;
116 $integration->save();
117
118 do_action('fluent_community/product_integration_feed_updated', $integration->id, $space->id);
119
120 return;
121 }
122 }
123
124 public function maybeAddPaywallField($fields, $space)
125 {
126 $paywallField = [
127 'hidden' => true,
128 'type' => 'paywall',
129 'label' => __('Paywalls', 'fluent-community'),
130 'name' => 'paywall',
131 'paywalls' => [],
132 'text_color' => '#FFFFFF',
133 'button_text' => __('Join', 'fluent-community'),
134 'button_color' => '#2B2E33',
135 'background_color' => '',
136 'button_text_color' => '#FFFFFF',
137 'show_description' => 'no'
138 ];
139
140 $existingNames = array_column($fields, 'name');
141
142 if ($space->hasPaywallIntegration()) {
143 if (!in_array('paywall', $existingNames)) {
144 $actionIndex = array_search('action', $existingNames);
145 if ($actionIndex !== false) {
146 array_splice($fields, $actionIndex, 0, [$paywallField]);
147 } else {
148 $fields[] = $paywallField;
149 }
150 }
151 } else {
152 if (in_array('paywall', $existingNames)) {
153 $fields = array_values(array_filter($fields, function($field) {
154 return $field['name'] != 'paywall';
155 }));
156 }
157 }
158
159 return $fields;
160 }
161
162 public function maybeFormatPaywallField($field, $value)
163 {
164 if ($value['type'] != 'paywall') {
165 return $value;
166 }
167
168 $value['paywalls'] = array_values(array_filter(
169 array_map(function($paywall) {
170 return sanitize_text_field($paywall);
171 }, $value['paywalls'] ?? []),
172 function($paywall) {
173 return !empty($paywall) && is_numeric($paywall);
174 }
175 ));
176
177 $value['show_description'] = Arr::get($value, 'show_description') == 'yes' ? 'yes' : 'no';
178
179 return $value;
180 }
181 }
182