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

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

444 lines 15.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\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 Helper::addToSpace($space, get_current_user_id(), 'admin');
97 }
98
99 return true;
100 }
101
102 protected static function createCourseTemplate()
103 {
104 $spaceGroupData = [
105 'title' => 'Courses',
106 'slug' => 'courses',
107 'description' => 'Course Learning Modules',
108 'status' => 'active',
109 'type' => 'space_group',
110 'settings' => [
111 'always_show_spaces' => 'yes'
112 ],
113 'serial' => 2
114 ];
115
116 $spaceGroup = SpaceGroup::where('slug', 'courses')->first();
117
118 if (!$spaceGroup) {
119 $spaceGroup = SpaceGroup::create($spaceGroupData);
120 } else {
121 $spaceGroup = SpaceGroup::create($spaceGroupData);
122 }
123
124 $coursesData = [
125 [
126 'title' => 'Demo Course 1',
127 'slug' => 'course-1',
128 'privacy' => 'private',
129 'status' => 'draft',
130 'description' => '',
131 'settings' => [
132 'course_type' => 'self_paced',
133 'emoji' => '1️⃣',
134 'shape_svg' => ''
135 ],
136 'parent_id' => $spaceGroup->id,
137 'serial' => 1
138 ],
139 [
140 'title' => 'Module 2',
141 'slug' => 'module-2',
142 'privacy' => 'private',
143 'status' => 'draft',
144 'description' => '',
145 'settings' => [
146 'course_type' => 'self_paced',
147 'emoji' => '2️⃣',
148 'shape_svg' => ''
149 ],
150 'parent_id' => $spaceGroup->id,
151 'serial' => 2
152 ]
153 ];
154
155 foreach ($coursesData as $courseData) {
156 $course = Course::where('slug', $courseData['slug'])->first();
157 if ($course) {
158 continue;
159 }
160
161 Course::create($courseData);
162 }
163 }
164
165 protected static function createProductTemplate()
166 {
167 $spaceGroupData = [
168 'title' => 'Product Discussions',
169 'slug' => 'product-discussions',
170 'description' => 'Product Discussion Group',
171 'status' => 'active',
172 'type' => 'space_group',
173 'settings' => [
174 'always_show_spaces' => 'yes'
175 ],
176 'serial' => 2
177 ];
178
179 $spaceGroup = SpaceGroup::where('slug', 'product-discussions')->first();
180
181 if (!$spaceGroup) {
182 $spaceGroup = SpaceGroup::create($spaceGroupData);
183 }
184
185 $spacesData = [
186 [
187 'title' => 'Give Feedback',
188 'slug' => 'give-feedback',
189 'privacy' => 'public',
190 'description' => '',
191 'settings' => [
192 'restricted_post_only' => 'no',
193 'emoji' => '💬',
194 'can_request_join' => 'yes',
195 'custom_lock_screen' => 'no',
196 'layout_style' => 'timeline',
197 'show_sidebar' => 'yes'
198 ],
199 'parent_id' => $spaceGroup->id,
200 'serial' => 1
201 ],
202 [
203 'title' => 'Ask for Help',
204 'slug' => 'ask-for-help',
205 'privacy' => 'public',
206 'description' => '',
207 'settings' => [
208 'restricted_post_only' => 'no',
209 'emoji' => '💬',
210 'can_request_join' => 'yes',
211 'custom_lock_screen' => 'no',
212 'layout_style' => 'timeline',
213 'show_sidebar' => 'yes'
214 ],
215 'parent_id' => $spaceGroup->id,
216 'serial' => 2
217 ],
218 [
219 'title' => 'Announcements',
220 'slug' => 'announcements',
221 'privacy' => 'public',
222 'description' => '',
223 'settings' => [
224 'restricted_post_only' => 'no',
225 'emoji' => '📣',
226 'can_request_join' => 'yes',
227 'custom_lock_screen' => 'no',
228 'layout_style' => 'timeline',
229 'show_sidebar' => 'yes'
230 ],
231 'parent_id' => $spaceGroup->id,
232 'serial' => 3
233 ],
234 ];
235
236 foreach ($spacesData as $spaceData) {
237 $space = Space::where('slug', $spaceData['slug'])->first();
238 if ($space) {
239 continue;
240 }
241
242 $space = Space::create($spaceData);
243 Helper::addToSpace($space, get_current_user_id(), 'admin');
244 }
245 }
246
247 public static function installAddons($addons = [])
248 {
249 $validAddons = ['fluent-crm', 'fluent-smtp', 'fluent-cart'];
250 $validAddons = array_intersect($validAddons, $addons);
251
252 if (!$validAddons || !current_user_can('install_plugins')) {
253 return;
254 }
255
256 foreach ($validAddons as $addon) {
257 self::installPlugin($addon);
258 }
259
260 return true;
261 }
262
263 public static function maybeOptinUserToNewsletter($settings)
264 {
265 $isOptin = isset($settings['subscribe_to_newsletter']) && $settings['subscribe_to_newsletter'] === 'yes';
266 if (!$isOptin) {
267 return false;
268 }
269
270 $userFullName = Arr::get($settings, 'user_full_name');
271 $userEmail = Arr::get($settings, 'user_email_address');
272
273 if (!$userEmail) {
274 return;
275 }
276
277 $url = 'https://fluentcommunity.co/discount-deal/?fluentcrm=1&route=contact&hash=8850223d-c62d-4e6a-8108-b04c7e9e4fdb';
278
279 $response = wp_remote_post($url, [
280 'body' => json_encode([ // phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode
281 'full_name' => $userFullName,
282 'email' => $userEmail,
283 'source' => 'fcom_plugin',
284 'optin_website' => home_url(),
285 'share_essential' => Arr::get($settings, 'share_data', 'no') === 'yes' ? 'yes' : 'no',
286 ])
287 ]);
288
289 return true;
290 }
291
292 private static function installPlugin($pluginSlug)
293 {
294 $plugin = [
295 'name' => $pluginSlug,
296 'repo-slug' => $pluginSlug,
297 'file' => $pluginSlug . '.php'
298 ];
299
300 $UrlMaps = [
301 'fluentform' => [
302 'admin_url' => admin_url('admin.php?page=fluent_forms'),
303 'title' => 'Go to Fluent Forms Dashboard',
304 ],
305 'fluent-crm' => [
306 'admin_url' => admin_url('admin.php?page=fluentcrm-admin'),
307 'title' => 'Go to FluentCRM Dashboard'
308 ],
309 'fluent-smtp' => [
310 'admin_url' => admin_url('options-general.php?page=fluent-mail#/'),
311 'title' => 'Go to FluentSMTP Dashboard'
312 ],
313 'fluent-cart' => [
314 'admin_url' => admin_url('admin.php?page=fluent-cart#/'),
315 'title' => 'Go to FluentCart Dashboard'
316 ]
317 ];
318
319 if (!isset($UrlMaps[$pluginSlug]) || (defined('DISALLOW_FILE_MODS') && DISALLOW_FILE_MODS)) {
320 return new \WP_Error('invalid_plugin', __('Invalid plugin or file mods are disabled.', 'fluent-community'));
321 }
322
323 try {
324 return self::backgroundInstaller($plugin);
325 } catch (\Exception $exception) {
326 return new \WP_Error('plugin_install_error', $exception->getMessage());
327 }
328 }
329
330 private static function backgroundInstaller($plugin_to_install)
331 {
332 if (!empty($plugin_to_install['repo-slug'])) {
333 require_once ABSPATH . 'wp-admin/includes/file.php';
334 require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
335 require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
336 require_once ABSPATH . 'wp-admin/includes/plugin.php';
337
338 WP_Filesystem();
339
340 $skin = new \Automatic_Upgrader_Skin();
341 $upgrader = new \WP_Upgrader($skin);
342 $installed_plugins = array_keys(\get_plugins());
343 $plugin_slug = $plugin_to_install['repo-slug'];
344 $plugin_file = isset($plugin_to_install['file']) ? $plugin_to_install['file'] : $plugin_slug . '.php';
345 $installed = false;
346 $activate = false;
347
348 // See if the plugin is installed already.
349 if (isset($installed_plugins[$plugin_file])) {
350 $installed = true;
351 $activate = !is_plugin_active($installed_plugins[$plugin_file]);
352 }
353
354 // Install this thing!
355 if (!$installed) {
356 // Suppress feedback.
357 ob_start();
358
359 try {
360 $plugin_information = plugins_api(
361 'plugin_information',
362 array(
363 'slug' => $plugin_slug,
364 'fields' => array(
365 'short_description' => false,
366 'sections' => false,
367 'requires' => false,
368 'rating' => false,
369 'ratings' => false,
370 'downloaded' => false,
371 'last_updated' => false,
372 'added' => false,
373 'tags' => false,
374 'homepage' => false,
375 'donate_link' => false,
376 'author_profile' => false,
377 'author' => false,
378 ),
379 )
380 );
381
382 if (is_wp_error($plugin_information)) {
383 throw new \Exception(wp_kses_post($plugin_information->get_error_message()));
384 }
385
386 $package = $plugin_information->download_link;
387 $download = $upgrader->download_package($package);
388
389 if (is_wp_error($download)) {
390 throw new \Exception(wp_kses_post($download->get_error_message()));
391 }
392
393 $working_dir = $upgrader->unpack_package($download, true);
394
395 if (is_wp_error($working_dir)) {
396 throw new \Exception(wp_kses_post($working_dir->get_error_message()));
397 }
398
399 $result = $upgrader->install_package(
400 array(
401 'source' => $working_dir,
402 'destination' => WP_PLUGIN_DIR,
403 'clear_destination' => false,
404 'abort_if_destination_exists' => false,
405 'clear_working' => true,
406 'hook_extra' => array(
407 'type' => 'plugin',
408 'action' => 'install',
409 ),
410 )
411 );
412
413 if (is_wp_error($result)) {
414 throw new \Exception(wp_kses_post($result->get_error_message()));
415 }
416
417 $activate = true;
418
419 } catch (\Exception $e) {
420 throw new \Exception(esc_html($e->getMessage()));
421 }
422
423 // Discard feedback.
424 ob_end_clean();
425 }
426
427 wp_clean_plugins_cache();
428
429 // Activate this thing.
430 if ($activate) {
431 try {
432 $result = activate_plugin($installed ? $installed_plugins[$plugin_file] : $plugin_slug . '/' . $plugin_file);
433
434 if (is_wp_error($result)) {
435 throw new \Exception(esc_html($result->get_error_message()));
436 }
437 } catch (\Exception $e) {
438 throw new \Exception(esc_html($e->getMessage()));
439 }
440 }
441 }
442 }
443 }
444