PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 2.9.1
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v2.9.1
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.9.1, at app/Services/OnboardingService.php

444 lines 15.3 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 /**
335 * Install and activate a plugin. Resolves the package from wordpress.org
336 * unless $downloadUrl is given, for plugins hosted outside the repo.
337 */
338 public static function backgroundInstaller($plugin_to_install, $downloadUrl = null)
339 {
340 if (empty($plugin_to_install['repo-slug'])) {
341 return;
342 }
343
344 require_once ABSPATH . 'wp-admin/includes/file.php';
345 require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
346 require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
347 require_once ABSPATH . 'wp-admin/includes/plugin.php';
348
349 WP_Filesystem();
350
351 $installedPlugins = array_reduce(array_keys(\get_plugins()), array(self::class, 'associate_plugin_file'), array());
352
353 $pluginSlug = $plugin_to_install['repo-slug'];
354 $pluginFile = isset($plugin_to_install['file']) ? $plugin_to_install['file'] : $pluginSlug . '.php';
355 $isInstalled = isset($installedPlugins[$pluginFile]);
356
357 if ($isInstalled) {
358 $needsActivation = !is_plugin_active($installedPlugins[$pluginFile]);
359 } else {
360 $upgrader = new \WP_Upgrader(new \Automatic_Upgrader_Skin());
361
362 // The upgrader prints progress markup that would corrupt the response.
363 ob_start();
364
365 try {
366 $package = $downloadUrl;
367
368 if (!$package) {
369 $information = plugins_api('plugin_information', array(
370 'slug' => $pluginSlug,
371 'fields' => array(
372 'short_description' => false,
373 'sections' => false,
374 'requires' => false,
375 'rating' => false,
376 'ratings' => false,
377 'downloaded' => false,
378 'last_updated' => false,
379 'added' => false,
380 'tags' => false,
381 'homepage' => false,
382 'donate_link' => false,
383 'author_profile' => false,
384 'author' => false,
385 ),
386 ));
387 if (is_wp_error($information)) {
388 throw new \Exception(wp_kses_post($information->get_error_message()));
389 }
390
391 $package = $information->download_link;
392 }
393
394 $download = $upgrader->download_package($package);
395 if (is_wp_error($download)) {
396 throw new \Exception(wp_kses_post($download->get_error_message()));
397 }
398
399 $workingDir = $upgrader->unpack_package($download, true);
400 if (is_wp_error($workingDir)) {
401 throw new \Exception(wp_kses_post($workingDir->get_error_message()));
402 }
403
404 $installed = $upgrader->install_package(array(
405 'source' => $workingDir,
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 if (is_wp_error($installed)) {
416 throw new \Exception(wp_kses_post($installed->get_error_message()));
417 }
418 } finally {
419 ob_end_clean();
420 }
421
422 $needsActivation = true;
423 }
424
425 wp_clean_plugins_cache();
426
427 if (!$needsActivation) return;
428
429 $activated = activate_plugin($isInstalled ? $installedPlugins[$pluginFile] : $pluginSlug . '/' . $pluginFile);
430
431 if (is_wp_error($activated)) {
432 throw new \Exception(wp_kses_post($activated->get_error_message()));
433 }
434 }
435
436 private static function associate_plugin_file($plugins, $key)
437 {
438 $path = explode('/', $key);
439 $filename = end($path);
440 $plugins[$filename] = $key;
441 return $plugins;
442 }
443 }
444