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

441 lines 15.4 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\Functions\Utility;
6 use FluentCommunity\App\Models\Space;
7 use FluentCommunity\App\Models\SpaceGroup;
8 use FluentCommunity\Framework\Support\Arr;
9 use FluentCommunity\Modules\Course\Model\Course;
10
11 class OnboardingService
12 {
13 public static function maybeCreateSpaceTemplates($template = '')
14 {
15 $spaceCount = Space::count();
16 if ($spaceCount >= 2) {
17 return false;
18 }
19
20 if (!$template || $template == 'blank') {
21 return false;
22 }
23
24 self::createDefaultSpaceTemplate();
25
26 if ($template == 'course') {
27 self::createCourseTemplate();
28 } else if ($template == 'product') {
29 self::createProductTemplate();
30 }
31
32 return true;
33 }
34
35 protected static function createDefaultSpaceTemplate()
36 {
37 $spaceGroupData = [
38 'title' => 'Get Started',
39 'slug' => 'get-started',
40 'description' => 'General Discussion Group',
41 'status' => 'active',
42 'type' => 'space_group',
43 'settings' => [
44 'hide_members' => 'no',
45 'always_show_spaces' => 'yes'
46 ],
47 'serial' => 1
48 ];
49
50 $spaceGroup = SpaceGroup::where('slug', 'get-started')->first();
51 if (!$spaceGroup) {
52 $spaceGroup = SpaceGroup::create($spaceGroupData);
53 }
54
55 $spacesData = [
56 [
57 'title' => 'Start Here',
58 'slug' => 'start-here',
59 'privacy' => 'public',
60 'description' => '',
61 'settings' => [
62 'restricted_post_only' => 'no',
63 'emoji' => '🏠',
64 'can_request_join' => 'yes',
65 'custom_lock_screen' => 'no',
66 'layout_style' => 'timeline',
67 'show_sidebar' => 'yes'
68 ],
69 'parent_id' => $spaceGroup->id,
70 'serial' => 1
71 ],
72 [
73 'title' => 'Say Hello',
74 'slug' => 'say-hello',
75 'privacy' => 'public',
76 'description' => '',
77 'settings' => [
78 'restricted_post_only' => 'no',
79 'emoji' => '👋',
80 'can_request_join' => 'yes',
81 'custom_lock_screen' => 'no',
82 'layout_style' => 'timeline',
83 'show_sidebar' => 'yes'
84 ],
85 'parent_id' => $spaceGroup->id,
86 'serial' => 2
87 ]
88 ];
89
90 foreach ($spacesData as $spaceData) {
91 $space = Space::where('slug', $spaceData['slug'])->first();
92 if ($space) {
93 continue;
94 }
95
96 $space = Space::create($spaceData);
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 Helper::addToSpace($space, get_current_user_id(), 'admin');
245 }
246 }
247
248 public static function installAddons($addons = [])
249 {
250 $validAddons = ['fluent-crm', 'fluent-smtp'];
251 $validAddons = array_intersect($validAddons, $addons);
252
253 if (!$validAddons || !current_user_can('install_plugins')) {
254 return;
255 }
256
257 foreach ($addons as $addon) {
258 self::installPlugin($addon);
259 }
260
261 return true;
262 }
263
264 public static function maybeOptinUserToNewsletter($settings)
265 {
266 $isOptin = isset($settings['subscribe_to_newsletter']) && $settings['subscribe_to_newsletter'] === 'yes';
267 if (!$isOptin) {
268 return false;
269 }
270
271 $userFullName = Arr::get($settings, 'user_full_name');
272 $userEmail = Arr::get($settings, 'user_email_address');
273
274 if (!$userEmail) {
275 return;
276 }
277
278 $url = 'https://fluentcommunity.co/discount-deal/?fluentcrm=1&route=contact&hash=8850223d-c62d-4e6a-8108-b04c7e9e4fdb';
279
280 $response = wp_remote_post($url, [
281 'body' => json_encode([ // phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode
282 'full_name' => $userFullName,
283 'email' => $userEmail,
284 'source' => 'fcom_plugin',
285 'optin_website' => site_url(),
286 'share_essential' => Arr::get($settings, 'share_data', 'no') === 'yes' ? 'yes' : 'no',
287 ])
288 ]);
289
290 return true;
291 }
292
293 private static function installPlugin($pluginSlug)
294 {
295 $plugin = [
296 'name' => $pluginSlug,
297 'repo-slug' => $pluginSlug,
298 'file' => $pluginSlug . '.php'
299 ];
300
301 $UrlMaps = [
302 'fluentform' => [
303 'admin_url' => admin_url('admin.php?page=fluent_forms'),
304 'title' => 'Go to Fluent Forms Dashboard',
305 ],
306 'fluent-crm' => [
307 'admin_url' => admin_url('admin.php?page=fluentcrm-admin'),
308 'title' => 'Go to FluentCRM Dashboard'
309 ],
310 'ninja-smtp' => [
311 'admin_url' => admin_url('options-general.php?page=fluent-mail#/'),
312 'title' => 'Go to FluentSMTP Dashboard'
313 ]
314 ];
315
316 if (!isset($UrlMaps[$pluginSlug]) || (defined('DISALLOW_FILE_MODS') && DISALLOW_FILE_MODS)) {
317 return new \WP_Error('invalid_plugin', __('Invalid plugin or file mods are disabled.', 'fluent-community'));
318 }
319
320 try {
321 return self::backgroundInstaller($plugin);
322 } catch (\Exception $exception) {
323 return new \WP_Error('plugin_install_error', $exception->getMessage());
324 }
325 }
326
327 private static function backgroundInstaller($plugin_to_install)
328 {
329 if (!empty($plugin_to_install['repo-slug'])) {
330 require_once ABSPATH . 'wp-admin/includes/file.php';
331 require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
332 require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
333 require_once ABSPATH . 'wp-admin/includes/plugin.php';
334
335 WP_Filesystem();
336
337 $skin = new \Automatic_Upgrader_Skin();
338 $upgrader = new \WP_Upgrader($skin);
339 $installed_plugins = array_keys(\get_plugins());
340 $plugin_slug = $plugin_to_install['repo-slug'];
341 $plugin_file = isset($plugin_to_install['file']) ? $plugin_to_install['file'] : $plugin_slug . '.php';
342 $installed = false;
343 $activate = false;
344
345 // See if the plugin is installed already.
346 if (isset($installed_plugins[$plugin_file])) {
347 $installed = true;
348 $activate = !is_plugin_active($installed_plugins[$plugin_file]);
349 }
350
351 // Install this thing!
352 if (!$installed) {
353 // Suppress feedback.
354 ob_start();
355
356 try {
357 $plugin_information = plugins_api(
358 'plugin_information',
359 array(
360 'slug' => $plugin_slug,
361 'fields' => array(
362 'short_description' => false,
363 'sections' => false,
364 'requires' => false,
365 'rating' => false,
366 'ratings' => false,
367 'downloaded' => false,
368 'last_updated' => false,
369 'added' => false,
370 'tags' => false,
371 'homepage' => false,
372 'donate_link' => false,
373 'author_profile' => false,
374 'author' => false,
375 ),
376 )
377 );
378
379 if (is_wp_error($plugin_information)) {
380 throw new \Exception(wp_kses_post($plugin_information->get_error_message()));
381 }
382
383 $package = $plugin_information->download_link;
384 $download = $upgrader->download_package($package);
385
386 if (is_wp_error($download)) {
387 throw new \Exception(wp_kses_post($download->get_error_message()));
388 }
389
390 $working_dir = $upgrader->unpack_package($download, true);
391
392 if (is_wp_error($working_dir)) {
393 throw new \Exception(wp_kses_post($working_dir->get_error_message()));
394 }
395
396 $result = $upgrader->install_package(
397 array(
398 'source' => $working_dir,
399 'destination' => WP_PLUGIN_DIR,
400 'clear_destination' => false,
401 'abort_if_destination_exists' => false,
402 'clear_working' => true,
403 'hook_extra' => array(
404 'type' => 'plugin',
405 'action' => 'install',
406 ),
407 )
408 );
409
410 if (is_wp_error($result)) {
411 throw new \Exception(wp_kses_post($result->get_error_message()));
412 }
413
414 $activate = true;
415
416 } catch (\Exception $e) {
417 throw new \Exception(esc_html($e->getMessage()));
418 }
419
420 // Discard feedback.
421 ob_end_clean();
422 }
423
424 wp_clean_plugins_cache();
425
426 // Activate this thing.
427 if ($activate) {
428 try {
429 $result = activate_plugin($installed ? $installed_plugins[$plugin_file] : $plugin_slug . '/' . $plugin_file);
430
431 if (is_wp_error($result)) {
432 throw new \Exception(esc_html($result->get_error_message()));
433 }
434 } catch (\Exception $e) {
435 throw new \Exception(esc_html($e->getMessage()));
436 }
437 }
438 }
439 }
440 }
441