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 / app / Services / OnboardingService.php

OnboardingService.php in FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses 2.10.01, at app/Services/OnboardingService.php

449 lines 15.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCommunity\App\Services;
4
5 use FluentCommunity\App\Models\Space;
6 use FluentCommunity\App\Models\SpaceGroup;
7 use FluentCommunity\Framework\Support\Arr;
8 use FluentCommunity\Modules\Course\Model\Course;
9
10 class OnboardingService
11 {
12 public static function maybeCreateSpaceTemplates($template = '')
13 {
14 $spaceCount = Space::count();
15 if ($spaceCount >= 2) {
16 return false;
17 }
18
19 if (!$template || $template == 'blank') {
20 return false;
21 }
22
23 self::createDefaultSpaceTemplate();
24
25 if ($template == 'course') {
26 self::createCourseTemplate();
27 } else if ($template == 'product') {
28 self::createProductTemplate();
29 }
30
31 return true;
32 }
33
34 protected static function createDefaultSpaceTemplate()
35 {
36 $spaceGroupData = [
37 'title' => 'Get Started',
38 'slug' => 'get-started',
39 'description' => 'General Discussion Group',
40 'status' => 'active',
41 'type' => 'space_group',
42 'settings' => [
43 'hide_members' => 'no',
44 'always_show_spaces' => 'yes'
45 ],
46 'serial' => 1
47 ];
48
49 $spaceGroup = SpaceGroup::where('slug', 'get-started')->first();
50 if (!$spaceGroup) {
51 $spaceGroup = SpaceGroup::create($spaceGroupData);
52 }
53
54 $spacesData = [
55 [
56 'title' => 'Start Here',
57 'slug' => 'start-here',
58 'privacy' => 'public',
59 'description' => '',
60 'settings' => [
61 'restricted_post_only' => 'no',
62 'emoji' => '🏠',
63 'can_request_join' => 'yes',
64 'custom_lock_screen' => 'no',
65 'layout_style' => 'timeline',
66 'show_sidebar' => 'yes'
67 ],
68 'parent_id' => $spaceGroup->id,
69 'serial' => 1
70 ],
71 [
72 'title' => 'Say Hello',
73 'slug' => 'say-hello',
74 'privacy' => 'public',
75 'description' => '',
76 'settings' => [
77 'restricted_post_only' => 'no',
78 'emoji' => '👋',
79 'can_request_join' => 'yes',
80 'custom_lock_screen' => 'no',
81 'layout_style' => 'timeline',
82 'show_sidebar' => 'yes'
83 ],
84 'parent_id' => $spaceGroup->id,
85 'serial' => 2
86 ]
87 ];
88
89 foreach ($spacesData as $spaceData) {
90 $space = Space::where('slug', $spaceData['slug'])->first();
91 if ($space) {
92 continue;
93 }
94
95 $space = Space::create($spaceData);
96 /** @var Space $space */
97 Helper::addToSpace($space, get_current_user_id(), 'admin');
98 }
99
100 return true;
101 }
102
103 protected static function createCourseTemplate()
104 {
105 $spaceGroupData = [
106 'title' => 'Courses',
107 'slug' => 'courses',
108 'description' => 'Course Learning Modules',
109 'status' => 'active',
110 'type' => 'space_group',
111 'settings' => [
112 'always_show_spaces' => 'yes'
113 ],
114 'serial' => 2
115 ];
116
117 $spaceGroup = SpaceGroup::where('slug', 'courses')->first();
118
119 if (!$spaceGroup) {
120 $spaceGroup = SpaceGroup::create($spaceGroupData);
121 } else {
122 $spaceGroup = SpaceGroup::create($spaceGroupData);
123 }
124
125 $coursesData = [
126 [
127 'title' => 'Demo Course 1',
128 'slug' => 'course-1',
129 'privacy' => 'private',
130 'status' => 'draft',
131 'description' => '',
132 'settings' => [
133 'course_type' => 'self_paced',
134 'emoji' => '1️⃣',
135 'shape_svg' => ''
136 ],
137 'parent_id' => $spaceGroup->id,
138 'serial' => 1
139 ],
140 [
141 'title' => 'Module 2',
142 'slug' => 'module-2',
143 'privacy' => 'private',
144 'status' => 'draft',
145 'description' => '',
146 'settings' => [
147 'course_type' => 'self_paced',
148 'emoji' => '2️⃣',
149 'shape_svg' => ''
150 ],
151 'parent_id' => $spaceGroup->id,
152 'serial' => 2
153 ]
154 ];
155
156 foreach ($coursesData as $courseData) {
157 $course = Course::where('slug', $courseData['slug'])->first();
158 if ($course) {
159 continue;
160 }
161
162 Course::create($courseData);
163 }
164 }
165
166 protected static function createProductTemplate()
167 {
168 $spaceGroupData = [
169 'title' => 'Product Discussions',
170 'slug' => 'product-discussions',
171 'description' => 'Product Discussion Group',
172 'status' => 'active',
173 'type' => 'space_group',
174 'settings' => [
175 'always_show_spaces' => 'yes'
176 ],
177 'serial' => 2
178 ];
179
180 $spaceGroup = SpaceGroup::where('slug', 'product-discussions')->first();
181
182 if (!$spaceGroup) {
183 $spaceGroup = SpaceGroup::create($spaceGroupData);
184 }
185
186 $spacesData = [
187 [
188 'title' => 'Give Feedback',
189 'slug' => 'give-feedback',
190 'privacy' => 'public',
191 'description' => '',
192 'settings' => [
193 'restricted_post_only' => 'no',
194 'emoji' => '💬',
195 'can_request_join' => 'yes',
196 'custom_lock_screen' => 'no',
197 'layout_style' => 'timeline',
198 'show_sidebar' => 'yes'
199 ],
200 'parent_id' => $spaceGroup->id,
201 'serial' => 1
202 ],
203 [
204 'title' => 'Ask for Help',
205 'slug' => 'ask-for-help',
206 'privacy' => 'public',
207 'description' => '',
208 'settings' => [
209 'restricted_post_only' => 'no',
210 'emoji' => '💬',
211 'can_request_join' => 'yes',
212 'custom_lock_screen' => 'no',
213 'layout_style' => 'timeline',
214 'show_sidebar' => 'yes'
215 ],
216 'parent_id' => $spaceGroup->id,
217 'serial' => 2
218 ],
219 [
220 'title' => 'Announcements',
221 'slug' => 'announcements',
222 'privacy' => 'public',
223 'description' => '',
224 'settings' => [
225 'restricted_post_only' => 'no',
226 'emoji' => '📣',
227 'can_request_join' => 'yes',
228 'custom_lock_screen' => 'no',
229 'layout_style' => 'timeline',
230 'show_sidebar' => 'yes'
231 ],
232 'parent_id' => $spaceGroup->id,
233 'serial' => 3
234 ],
235 ];
236
237 foreach ($spacesData as $spaceData) {
238 $space = Space::where('slug', $spaceData['slug'])->first();
239 if ($space) {
240 continue;
241 }
242
243 $space = Space::create($spaceData);
244 /** @var Space $space */
245 Helper::addToSpace($space, get_current_user_id(), 'admin');
246 }
247 }
248
249 public static function installAddons($addons = [])
250 {
251 $validAddons = ['fluent-crm', 'fluent-smtp', 'fluent-cart'];
252 $validAddons = array_intersect($validAddons, $addons);
253
254 if (!$validAddons || !current_user_can('install_plugins')) {
255 return;
256 }
257
258 foreach ($validAddons as $addon) {
259 self::installPlugin($addon);
260 }
261
262 return true;
263 }
264
265 public static function maybeOptinUserToNewsletter($settings)
266 {
267 $isOptin = isset($settings['subscribe_to_newsletter']) && $settings['subscribe_to_newsletter'] === 'yes';
268 if (!$isOptin) {
269 return false;
270 }
271
272 $userFullName = Arr::get($settings, 'user_full_name');
273 $userEmail = Arr::get($settings, 'user_email_address');
274
275 if (!$userEmail) {
276 return;
277 }
278
279 $url = 'https://fluentcommunity.co/discount-deal/?fluentcrm=1&route=contact&hash=8850223d-c62d-4e6a-8108-b04c7e9e4fdb';
280
281 $response = wp_safe_remote_post($url, [
282 'timeout' => 10,
283 'redirection' => 0,
284 'body' => json_encode([ // phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode
285 'full_name' => $userFullName,
286 'email' => $userEmail,
287 'source' => 'fcom_plugin',
288 'optin_website' => home_url(),
289 'share_essential' => Arr::get($settings, 'share_data', 'no') === 'yes' ? 'yes' : 'no',
290 ])
291 ]);
292
293 return true;
294 }
295
296 private static function installPlugin($pluginSlug)
297 {
298 $plugin = [
299 'name' => $pluginSlug,
300 'repo-slug' => $pluginSlug,
301 'file' => $pluginSlug . '.php'
302 ];
303
304 $UrlMaps = [
305 'fluentform' => [
306 'admin_url' => admin_url('admin.php?page=fluent_forms'),
307 'title' => 'Go to Fluent Forms Dashboard',
308 ],
309 'fluent-crm' => [
310 'admin_url' => admin_url('admin.php?page=fluentcrm-admin'),
311 'title' => 'Go to FluentCRM Dashboard'
312 ],
313 'fluent-smtp' => [
314 'admin_url' => admin_url('options-general.php?page=fluent-mail#/'),
315 'title' => 'Go to FluentSMTP Dashboard'
316 ],
317 'fluent-cart' => [
318 'admin_url' => admin_url('admin.php?page=fluent-cart#/'),
319 'title' => 'Go to FluentCart Dashboard'
320 ]
321 ];
322
323 if (!isset($UrlMaps[$pluginSlug]) || (defined('DISALLOW_FILE_MODS') && DISALLOW_FILE_MODS)) {
324 return new \WP_Error('invalid_plugin', __('Invalid plugin or file mods are disabled.', 'fluent-community'));
325 }
326
327 try {
328 return self::backgroundInstaller($plugin);
329 } catch (\Exception $exception) {
330 return new \WP_Error('plugin_install_error', $exception->getMessage());
331 }
332 }
333
334 public static function backgroundInstaller($plugin_to_install)
335 {
336 if (!empty($plugin_to_install['repo-slug'])) {
337 require_once ABSPATH . 'wp-admin/includes/file.php';
338 require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
339 require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
340 require_once ABSPATH . 'wp-admin/includes/plugin.php';
341
342 WP_Filesystem();
343
344 $skin = new \Automatic_Upgrader_Skin();
345 $upgrader = new \WP_Upgrader($skin);
346 $installed_plugins = array_reduce(array_keys(\get_plugins()), array(self::class, 'associate_plugin_file'), array());
347 $plugin_slug = $plugin_to_install['repo-slug'];
348 $plugin_file = isset($plugin_to_install['file']) ? $plugin_to_install['file'] : $plugin_slug . '.php';
349 $installed = false;
350 $activate = false;
351
352 // See if the plugin is installed already.
353 if (isset($installed_plugins[$plugin_file])) {
354 $installed = true;
355 $activate = !is_plugin_active($installed_plugins[$plugin_file]);
356 }
357
358 // Install this thing!
359 if (!$installed) {
360 // Suppress feedback.
361 ob_start();
362
363 try {
364 $plugin_information = plugins_api(
365 'plugin_information',
366 array(
367 'slug' => $plugin_slug,
368 'fields' => array(
369 'short_description' => false,
370 'sections' => false,
371 'requires' => false,
372 'rating' => false,
373 'ratings' => false,
374 'downloaded' => false,
375 'last_updated' => false,
376 'added' => false,
377 'tags' => false,
378 'homepage' => false,
379 'donate_link' => false,
380 'author_profile' => false,
381 'author' => false,
382 ),
383 )
384 );
385
386 if (is_wp_error($plugin_information)) {
387 throw new \Exception(wp_kses_post($plugin_information->get_error_message()));
388 }
389
390 $package = $plugin_information->download_link;
391 $download = $upgrader->download_package($package);
392
393 if (is_wp_error($download)) {
394 throw new \Exception(wp_kses_post($download->get_error_message()));
395 }
396
397 $working_dir = $upgrader->unpack_package($download, true);
398
399 if (is_wp_error($working_dir)) {
400 throw new \Exception(wp_kses_post($working_dir->get_error_message()));
401 }
402
403 $result = $upgrader->install_package(
404 array(
405 'source' => $working_dir,
406 'destination' => WP_PLUGIN_DIR,
407 'clear_destination' => false,
408 'abort_if_destination_exists' => false,
409 'clear_working' => true,
410 'hook_extra' => array(
411 'type' => 'plugin',
412 'action' => 'install',
413 ),
414 )
415 );
416
417 if (is_wp_error($result)) {
418 throw new \Exception(wp_kses_post($result->get_error_message()));
419 }
420
421 $activate = true;
422 } finally {
423 // Discard feedback.
424 ob_end_clean();
425 }
426 }
427
428 wp_clean_plugins_cache();
429
430 // Activate this thing.
431 if ($activate) {
432 $result = activate_plugin($installed ? $installed_plugins[$plugin_file] : $plugin_slug . '/' . $plugin_file);
433
434 if (is_wp_error($result)) {
435 throw new \Exception(wp_kses_post($result->get_error_message()));
436 }
437 }
438 }
439 }
440
441 private static function associate_plugin_file($plugins, $key)
442 {
443 $path = explode('/', $key);
444 $filename = end($path);
445 $plugins[$filename] = $key;
446 return $plugins;
447 }
448 }
449