PluginProbe
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration / 1.12
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration v1.12
2.0.15 2.0.12 2.0.10 2.0.4 2.0.1 2.0.0 1.95.3 1.95.2 1.95 1.91.6 trunk 1.11 1.12 1.13 1.20 1.21 1.22 1.23 1.30 1.31 1.32 1.35 1.40 1.41 1.45 All 41 releases
fluent-boards / app / Http / Controllers / OptionsController.php

OptionsController.php in FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration 1.12, at app/Http/Controllers/OptionsController.php

827 lines 30.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentBoards\App\Http\Controllers;
4
5 use FluentBoards\App\App;
6 use FluentBoards\App\Models\Meta;
7 use FluentBoards\App\Models\Task;
8 use FluentBoards\App\Models\User;
9 use FluentBoards\App\Models\Board;
10 use FluentBoards\App\Models\Relation;
11 use FluentBoards\App\Services\BoardService;
12 use FluentBoards\App\Services\Helper;
13 use FluentBoards\App\Services\OptionService;
14 use FluentBoards\App\Services\Constant;
15 use FluentBoards\App\Services\PermissionManager;
16 use FluentBoards\Framework\Http\Request\Request;
17 use FluentBoards\Framework\Support\Arr;
18
19 class OptionsController extends Controller
20 {
21 private $boardService;
22 private $optionService;
23
24 public function __construct(BoardService $boardService, OptionService $optionService)
25 {
26 parent::__construct();
27 $this->boardService = $boardService;
28 $this->optionService = $optionService;
29 }
30
31 public function selectorOptions(Request $request)
32 {
33 try {
34 $optionKey = $request->getSafe('option_key');
35 $search = $request->getSafe('search');
36 $includedIds = $request->getSafe('values');
37 $boardId = $request->getSafe('board_id');
38
39 $options = [];
40 if ('users' === $optionKey || 'task_assignees' === $optionKey) { // no ajax/code is designed to handle this eventually will goto else
41
42 if (!PermissionManager::isBoardManager($boardId)) {
43 throw new \Exception('You do not have permission to access this route');
44 }
45
46 if (!defined('FLUENT_BOARDS_PRO')) {
47 // get who has 'manage_options' capability
48 $users = PermissionManager::getAll_WP_Admins();
49
50 } else {
51 $users = User::query()
52 ->when($search, function ($query) use ($search) {
53 return $query->where('display_name', 'LIKE', '%' . $search . '%')->orWhere('user_email', 'LIKE', '%' . $search . '%');
54 })
55 ->limit(20)->get();
56 }
57
58 $options = $this->addUserDataAsSelectorOption($users);
59
60 } elseif ('boards' === $optionKey) {
61 $boards = Board::query()
62 ->when($search, function ($query) use ($search) {
63 return $query->where('title', 'LIKE', '%' . $search . '%');
64 })->take(20)->get();
65
66 foreach ($boards as $board) {
67 $options[] = [
68 'id' => $board->id,
69 'title' => $board->title,
70 'left_side_value' => $board->title,
71 'right_side_value' => $board->slug,
72 ];
73 }
74 } elseif ('assigned_in_task' == $optionKey) {
75 $users = (new BoardService())->getAssigneesByBoard($boardId, $search);
76 $options = $this->addUserDataAsSelectorOption($users);
77 } else {
78 $options = apply_filters('fluent_boards/ajax_options_' . $optionKey, [], $search, $includedIds);
79 }
80
81 return $this->sendSuccess([
82 'options' => $options,
83 ], 200);
84 } catch (\Exception $e) {
85 return $this->sendError($e->getMessage(), 404);
86 }
87 }
88
89 private function addUserDataAsSelectorOption($users)
90 {
91 $options = [];
92 foreach ($users as $user) {
93 $options[] = [
94 'id' => $user->ID,
95 'title' => $user->display_name . ' (' . $user->user_email . ')',
96 'photo' => get_avatar_url($user->user_email),
97 'left_side_value' => $user->display_name,
98 'right_side_value' => $user->user_email,
99 ];
100 }
101 return $options;
102 }
103
104 public function getCurrentUserPermissions()
105 {
106 try {
107 $currentUserBoards = Relation::query()
108 ->where('user_id', get_current_user_id())
109 ->whereNotNull('board_id')
110 ->where('status', 'ACTIVE')
111 ->get();
112 foreach ($currentUserBoards as &$currentUserBoardPermission) {
113 $currentUserBoardPermission->permissions = \maybe_unserialize($currentUserBoardPermission->permissions);
114 }
115
116 return $this->sendSuccess(
117 $currentUserBoards, 200
118 );
119 } catch (\Exception $e) {
120 return $this->sendError($e->getMessage(), 404);
121 }
122 }
123
124 public function getUserPermission(Request $request)
125 {
126 try {
127 $boardId = $request->getSafe('boardId');
128 $userId = $request->getSafe('userId');
129
130 $boardUser = Relation::where('board_id', $boardId)
131 ->where('user_id', $userId)
132 ->where('status', 'ACTIVE')->first();
133 if (!$boardUser->is_admin) {
134 $boardUser->permissions = \maybe_unserialize($boardUser->permissions);
135 }
136
137 return $this->sendSuccess(
138 $boardUser, 200
139 );
140 } catch (\Exception $e) {
141 return $this->sendError($e->getMessage(), 404);
142 }
143 }
144
145 public function updatedUserPermission(Request $request)
146 {
147 try {
148 $permission = $request->getSafe('userPermission');
149 $updateType = $request->getSafe('updateType');
150 $boardId = $request->getSafe('boardId');
151 $userId = $request->getSafe('userId');
152
153 $boardUser = Relation::where('board_id', $boardId)->where('user_id', $userId)->status('ACTIVE')->first();
154
155 if ('Board Admin' == $permission) {
156 $boardUser->is_admin = 'add' == $updateType ? 1 : 0;
157 $boardUser->save();
158 } else {
159 if (0 == $boardUser->is_admin) {
160 $permissionsAlreadyHave = maybe_unserialize($boardUser->permissions);
161 if (!in_array($permission, $permissionsAlreadyHave) && 'add' == $updateType) {
162 array_push($permissionsAlreadyHave, $permission);
163 } elseif (in_array($permission, $permissionsAlreadyHave) && 'remove' == $updateType) {
164 if (($key = array_search($permission, $permissionsAlreadyHave)) !== false) {
165 unset($permissionsAlreadyHave[$key]);
166 }
167 }
168 $boardUser->permissions = serialize($permissionsAlreadyHave);
169 $boardUser->save();
170 }
171 }
172
173 $boardUser->permissions = maybe_unserialize($boardUser->permissions);
174
175 return $this->sendSuccess(
176 $boardUser, 200
177 );
178 } catch (\Exception $e) {
179 return $this->sendError($e->getMessage(), 404);
180 }
181 }
182
183 public function SetUserSuperAdmin($userId)
184 {
185 try {
186 $this->optionService->createSuperAdmin($userId);
187 return $this->sendSuccess([
188 'message' => __('Member has been set super admin successfully!', 'fluent-boards')
189 ], 200);
190
191 } catch (\Exception $e) {
192 return $this->sendError($e->getMessage(), 404);
193 }
194 }
195
196 public function removeUserSuperAdmin($userId)
197 {
198 try {
199 $this->optionService->removeUserSuperAdmin($userId);
200
201 return $this->sendSuccess([
202 'message' => __('User has been removed as super admin successfully!', 'fluent-boards'),
203 ], 200);
204 } catch (\Exception $e) {
205 return $this->sendError($e->getMessage(), 404);
206 }
207 }
208
209 public function IsUserAllBoardAdmin(Request $request)
210 {
211 try {
212 $userId = $request->getSafe('id');
213 $isSuperAdmin = false;
214 $superAdmin = Relation::where('board_id', null)->where('user_id', $userId)->where('status', 'ACTIVE')->first();
215 $totalSuperAdmin = Relation::where('board_id', null)->where('status', 'ACTIVE')->count();
216 $permissions = [];
217 if ($superAdmin) {
218 $isSuperAdmin = true;
219 $permissions = \maybe_unserialize($superAdmin->permissions);
220 }
221
222 return $this->sendSuccess([
223 'allBoardAdmin' => $isSuperAdmin,
224 'permissions' => $permissions,
225 'numberOfSuperAdmin' => $totalSuperAdmin,
226 ], 200);
227 } catch (\Exception $e) {
228 return $this->sendError($e->getMessage(), 404);
229 }
230 }
231
232 public function RemoveUserFromSuperAdmin(Request $request, $id)
233 {
234 try {
235 $userId = $request->getSafe('id');
236
237 $superAdmin = Relation::where('board_id', null)->where('user_id', $userId)->first();
238 $superAdmin->status = 'INACTIVE';
239 $superAdmin->save();
240
241 return $this->sendSuccess([
242 'message' => __('User removed as super admin', 'fluent-boards'),
243 ], 201);
244 } catch (\Exception $e) {
245 return $this->sendError($e->getMessage(), 404);
246 }
247 }
248
249 public function removeUserFromBoard(Request $request)
250 {
251 try {
252 $boardId = $request->getSafe('boardId');
253 $userId = $request->getSafe('userId');
254
255 $this->boardService->removeUserFromBoard($boardId, $userId);
256
257 if (!PermissionManager::isAdmin($userId)) {
258 $this->boardService->removeFromRecentlyOpened($boardId, $userId);
259 }
260
261 return $this->sendSuccess([
262 'message' => __('User Removed from Board successfully!', 'fluent-boards'),
263 ], 201);
264 } catch (\Exception $e) {
265 return $this->sendError($e->getMessage(), 404);
266 }
267 }
268
269 public function addAsSuperAdmin(Request $request)
270 {
271 try {
272 $userIds = $request->getSafe('memberIds');
273 foreach ($userIds as $userId) {
274 $this->createSuperAdmin($userId);
275 }
276
277 return $this->sendSuccess([
278 'message' => __('Fluent boards admin added', 'fluent-boards'),
279 ], 201);
280 } catch (\Exception $e) {
281 return $this->sendError($e->getMessage(), 404);
282 }
283 }
284
285 private function createSuperAdmin($userId)
286 {
287 try {
288 $existUser = Meta::where('object_id', $userId)->first();
289 if (!$existUser) {
290 $meta = new Meta();
291 $meta->object_id = $userId;
292 $meta->object_type = Constant::FLUENT_BOARD_ADMIN;
293 $meta->save();
294 }
295 } catch (\Exception $e) {
296 return $this->sendError($e->getMessage(), 404);
297 }
298 }
299
300 public function addMembersInBoards(Request $request)
301 {
302 try {
303 $userIds = $request->getSafe('memberIds');
304 $boardIds = $request->getSafe('boardIds');
305
306 foreach ($userIds as $userId) {
307 foreach ($boardIds as $boardId) {
308 $this->boardService->addMembersInBoard($boardId, $userId);
309 }
310 }
311
312 return $this->sendSuccess([
313 'message' => __('Members added to boards', 'fluent-boards'),
314 ], 201);
315 } catch (\Exception $e) {
316 return $this->sendError($e->getMessage(), 404);
317 }
318 }
319
320
321 public function updateGlobalNotificationSettings(Request $request)
322 {
323 try {
324 $newSettings = $request->getSafe('updatedSettings');
325
326 $this->optionService->updateGlobalNotificationSettings($newSettings);
327
328 return $this->sendSuccess([
329 'message' => __("Notification settings are updated", 'fluent-boards'),
330 ], 201);
331 } catch (\Exception $e) {
332 return $this->sendError($e->getMessage(), 404);
333 }
334 }
335
336 public function getGlobalNotificationSettings()
337 {
338 try {
339 $globalSettings = $this->optionService->getGlobalNotificationSettings();
340 if ($globalSettings->value)
341 $currentSettings = maybe_unserialize($globalSettings->value);
342
343 return $this->sendSuccess([
344 'currentSettings' => $currentSettings,
345 ], 200);
346 } catch (\Exception $e) {
347 return $this->sendError($e->getMessage(), 404);
348 }
349 }
350
351 public function getBoardMembers(Request $request)
352 {
353 if (!PermissionManager::userHasAnyBoardAccess()) {
354 return $this->sendError([
355 'message' => __('You do not have permission to access this route', 'fluent-boards')
356 ]);
357 }
358
359 $boardId = $request->getSafe('boardId', 'intval');
360
361 $memberUserIds = Relation::where('object_type', 'board_user')
362 ->select(['foreign_id'])
363 ->groupBy('foreign_id');
364
365 if ($boardId) {
366 $memberUserIds = $memberUserIds->where('object_id', $boardId);
367 }
368
369 $members = [];
370
371 $memberUserIds = $memberUserIds->get()
372 ->pluck('foreign_id')->toArray();
373
374
375 if ($memberUserIds) {
376 $memberUsers = get_users([
377 'include' => $memberUserIds
378 ]);
379
380
381 foreach ($memberUsers as $memberUser) {
382 $name = trim($memberUser->first_name . ' ' . $memberUser->last_name);
383 if (!$name) {
384 $name = $memberUser->display_name;
385 }
386
387 $members[$memberUser->ID] = [
388 'ID' => $memberUser->ID,
389 'display_name' => $name,
390 'photo' => get_avatar_url($memberUser->user_email)
391 ];
392 }
393
394 }
395
396 $adminUsers = get_users([
397 'role' => 'administrator',
398 'exclude' => $memberUserIds
399 ]);
400
401 foreach ($adminUsers as $user) {
402 $name = trim($user->first_name . ' ' . $user->last_name);
403 if (!$name) {
404 $name = $user->display_name;
405 }
406 $members[$user->ID] = [
407 'ID' => $user->ID,
408 'display_name' => $name,
409 'photo' => get_avatar_url($user->user_email)
410 ];
411 }
412
413 $members = array_values($members);
414
415 // sort members by name
416 usort($members, function ($a, $b) {
417 return strcmp($a['display_name'], $b['display_name']);
418 });
419
420 return [
421 'members' => $members
422 ];
423 }
424
425 public function quickSearch()
426 {
427 $currentUserId = get_current_user_id();
428
429 $query = sanitize_text_field($_REQUEST['query']);
430 $query = strtolower($query);
431 $scope = sanitize_text_field($_REQUEST['scope']);
432
433 $tasksQuery = Task::query()->where('parent_id', null)->whereRaw('LOWER(title) LIKE ?', ['%' . $query . '%']);
434 $isUserAdmin = PermissionManager::isAdmin($currentUserId);
435
436 // This is a check for deleted tasks to be excluded from search results
437 $allActiveBoardsIds = Board::query()->where('archived_at', null)->pluck('id')->toArray();
438
439 if ($scope == 'all') {
440 $boardQuery = Board::query()->whereRaw('LOWER(title) LIKE ?', ['%' . $query . '%']);
441 if ($isUserAdmin) {
442 $boards = $boardQuery->get();
443 $tasks = $tasksQuery->get();
444 } else {
445 $boardIds = PermissionManager::getBoardIdsForUser($currentUserId);
446 $boards = $boardQuery->whereIn('id', $boardIds)->get();
447 $tasks = $tasksQuery->whereIn('board_id', $boardIds)->get();
448 }
449 } else {
450 $boards = []; // boards results is not needed in scoped search
451 $inBoard = (int)$scope;
452 if ($isUserAdmin || in_array($inBoard, $boardIds = PermissionManager::getBoardIdsForUser($currentUserId))) {
453 $tasks = $tasksQuery->where('board_id', $inBoard)->get();
454 } else {
455 // Out of permission scope search
456 $tasks = [];
457 }
458 }
459
460
461 $formattedTasks = [];
462 $formattedBoards = [];
463 foreach ($boards as $board) {
464 $formattedBoards[] = [
465 'type' => 'board',
466 'id' => $board->id,
467 'title' => $board->title,
468 'description' => $board->description,
469 'url' => Helper::getBoardUrl($board->id)
470 ];
471 }
472 foreach ($tasks as $task) {
473
474 if (!in_array($task->board_id, $allActiveBoardsIds)) {
475 // if the task is not in an active board, skip it
476 continue;
477 }
478
479 $board = $task->board;
480
481 $formattedTasks[] = [
482 'type' => 'task',
483 'id' => $task->id,
484 'title' => $task->title,
485 'description' => $task->description,
486 'url' => Helper::getTaskUrl($task->id, $board->id),
487 'board' => [
488 'id' => $board->id,
489 'title' => $board->title,
490 'url' => Helper::getBoardUrl($board->id)
491 ],
492 'stage' => [
493 'id' => $task->stage_id,
494 'title' => $task->stage->title ?? '',
495 ],
496
497 ];
498 }
499 return $this->sendSuccess([
500 'tasks' => $formattedTasks,
501 'boards' => $formattedBoards
502 ], 200);
503
504 }
505
506 public function getDashboardViewSettings()
507 {
508 try {
509 $globalSettings = $this->optionService->getDashboardViewSettings();
510 if ($globalSettings->value)
511 $currentSettings = maybe_unserialize($globalSettings->value);
512
513 return $this->sendSuccess([
514 'currentSettings' => $currentSettings,
515 ], 200);
516 } catch (\Exception $e) {
517 return $this->sendError($e->getMessage(), 404);
518 }
519 }
520
521 public function updateDashboardViewSettings(Request $request)
522 {
523 try {
524 $newSettings = $request->getSafe('updatedSettings');
525
526 $this->optionService->updateDashboardViewSettings($newSettings);
527
528 return $this->sendSuccess([
529 'message' => __("Dashboard view settings are updated", 'fluent-boards'),
530 ], 201);
531 } catch (\Exception $e) {
532 return $this->sendError($e->getMessage(), 404);
533 }
534 }
535
536
537 public function getAddonsSettings()
538 {
539 $addOns = [
540 'fluent-crm' => [
541 'title' => __('FluentCRM', 'fluent-boards'),
542 'logo' => fluent_boards_mix('images/addons/fluent-crm.svg'),
543 'is_installed' => defined('FLUENTCRM'),
544 'learn_more_url' => 'https://fluentcrm.com/',
545 'associate_doc' => 'https://fluentboards.com/docs/fluentboards-integration-with-fluentcrm/',
546 'action_text' => $this->isPluginInstalled('fluent-crm/fluent-crm.php') ? __('Activate FluentCRM', 'fluent-boards') : __('Install FluentCRM', 'fluent-boards'),
547 'description' => __('FluentCRM is a Self Hosted Email Marketing Automation Plugin for WordPress. Manage your leads and customers, email campaigns, automated email sequencing and many more', 'fluent-boards')
548 ],
549 'fluentform' => [
550 'title' => __('Fluent Forms', 'fluent-boards'),
551 'logo' => fluent_boards_mix('images/addons/fluentform.png'),
552 'is_installed' => defined('FLUENTFORM'),
553 'learn_more_url' => 'https://wordpress.org/plugins/fluentform/',
554 'associate_doc' => 'https://fluentboards.com/docs/fluentboards-integration-with-fluent-forms/',
555 'action_text' => $this->isPluginInstalled('fluent-form/fluent-form.php') ? __('Activate Fluent Forms', 'fluent-boards') : __('Install Fluent Forms', 'fluent-boards'),
556 'description' => __('Collect leads and build any type of forms, accept payments, connect with your CRM with the Fastest Contact Form Builder Plugin for WordPress', 'fluent-boards')
557 ],
558 'fluent-support' => [
559 'title' => __('Fluent Support', 'fluent-boards'),
560 'logo' => fluent_boards_mix('images/addons/fluent-support.svg'),
561 'is_installed' => defined('FLUENT_SUPPORT_VERSION'),
562 'learn_more_url' => 'https://wordpress.org/plugins/fluent-connect/',
563 'settings_url' => admin_url('admin.php?page=fluent-support#/'),
564 'associate_doc' => 'https://fluentboards.com/docs/fluentboards-integration-with-fluentsupport/',
565 'action_text' => $this->isPluginInstalled('fluent-support/fluent-support.php') ? __('Activate Fluent Support', 'fluent-boards') : __('Install Fluent Support', 'fluent-boards'),
566 'description' => __('WordPress Helpdesk and Customer Support Ticket Plugin. Provide awesome support and manage customer queries right from your WordPress dashboard.', 'fluent-boards')
567 ],
568 'fluent-smtp' => [
569 'title' => __('Fluent SMTP', 'fluent-boards'),
570 'logo' => fluent_boards_mix('images/addons/fluent-smtp.svg'),
571 'is_installed' => defined('FLUENTMAIL'),
572 'learn_more_url' => 'https://wordpress.org/plugins/fluent-smtp/',
573 'associate_doc' => admin_url('options-general.php?page=fluent-mail#/'),
574 'action_text' => $this->isPluginInstalled('fluent-smtp/fluent-smtp.php') ? __('Activate Fluent SMTP', 'fluent-boards') : __('Install Fluent SMTP', 'fluent-boards'),
575 'description' => __('The Ultimate SMTP and SES Plugin for WordPress. Connect with any SMTP, SendGrid, Mailgun, SES, Sendinblue, PepiPost, Google, Microsoft and more.', 'fluent-boards')
576 ],
577 ];
578
579 $modules = fluent_boards_get_pref_settings(false);
580
581 if (empty($modules['frontend']['render_type'])) {
582 $modules['frontend']['render_type'] = 'standalone';
583 }
584
585 $modules['panel_url'] = fluent_boards_page_url();
586
587 return [
588 'addons' => $addOns,
589 'featureModules' => $modules
590 ];
591 }
592
593 public function saveAddonsSettings(Request $request)
594 {
595 if (!defined('FLUENT_BOARDS_PRO')) {
596 return $this->sendError([
597 'message' => __('This feature is only available in Fluent Boards Pro', 'fluent-boards')
598 ]);
599 }
600
601 $settings = $request->get('settings', []);
602
603 $prefSettings = fluent_boards_get_pref_settings(false);
604
605 $settings = wp_parse_args($settings, $prefSettings);
606
607 $settings = Arr::only($settings, array_keys($prefSettings));
608 $settings['frontend']['slug'] = sanitize_title($settings['frontend']['slug']);
609
610 if (empty($settings['frontend']['slug'])) {
611 $settings['frontend']['slug'] = 'projects';
612 }
613
614 if (defined('FLUENT_BOARDS_SLUG') && FLUENT_BOARDS_SLUG) {
615 $settings['frontend']['slug'] = FLUENT_BOARDS_SLUG;
616 }
617
618 do_action('fluent_boards/saving_addons', $settings, $prefSettings);
619
620 update_option('fluent_boards_modules', $settings, 'yes');
621
622 return $this->sendSuccess([
623 'message' => __('Settings are saved', 'fluent-boards'),
624 'featureModules' => $settings
625 ]);
626 }
627
628 public function installPlugin(Request $request)
629 {
630 if (!current_user_can('install_plugins')) {
631 return $this->sendError([
632 'message' => __('Sorry! you do not have permission to install plugin', 'fluent-crm')
633 ]);
634 }
635
636 $plugin = $request->getSafe('plugin', 'sanitize_text_field');
637
638 $acceptedPlugins = [
639 'fluent-crm' => 'fluent-crm.php',
640 'fluentform' => 'fluentform.php',
641 'fluent-support' => 'fluent-support.php',
642 'fluent-smtp' => 'fluent-smtp.php'
643 ];
644
645 if (!isset($acceptedPlugins[$plugin])) {
646 return $this->sendError([
647 'message' => __('Invalid plugin', 'fluent-boards')
648 ]);
649 }
650
651
652 $pluginToInstall = [
653 'name' => __('Fluent Plugin', 'fluent-boards'),
654 'repo-slug' => $plugin,
655 'file' => $acceptedPlugins[$plugin],
656 ];
657
658 $this->backgroundInstaller($pluginToInstall, $plugin);
659
660 return $this->sendSuccess([
661 'message' => __('Plugin is being installed', 'fluent-boards')
662 ]);
663 }
664
665 private function isPluginInstalled($plugin)
666 {
667 return file_exists(WP_PLUGIN_DIR . '/' . $plugin);
668 }
669
670 private function backgroundInstaller($plugin_to_install, $plugin_id)
671 {
672 if (!empty($plugin_to_install['repo-slug'])) {
673 require_once ABSPATH . 'wp-admin/includes/file.php';
674 require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
675 require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
676 require_once ABSPATH . 'wp-admin/includes/plugin.php';
677
678 WP_Filesystem();
679
680 $skin = new \Automatic_Upgrader_Skin();
681 $upgrader = new \WP_Upgrader($skin);
682 $installed_plugins = array_reduce(array_keys(\get_plugins()), array($this, 'associate_plugin_file'), array());
683 $plugin_slug = $plugin_to_install['repo-slug'];
684 $plugin_file = isset($plugin_to_install['file']) ? $plugin_to_install['file'] : $plugin_slug . '.php';
685 $installed = false;
686 $activate = false;
687
688 // See if the plugin is installed already.
689 if (isset($installed_plugins[$plugin_file])) {
690 $installed = true;
691 $activate = !is_plugin_active($installed_plugins[$plugin_file]);
692 }
693
694 // Install this thing!
695 if (!$installed) {
696 // Suppress feedback.
697 ob_start();
698
699 try {
700 $plugin_information = plugins_api(
701 'plugin_information',
702 array(
703 'slug' => $plugin_slug,
704 'fields' => array(
705 'short_description' => false,
706 'sections' => false,
707 'requires' => false,
708 'rating' => false,
709 'ratings' => false,
710 'downloaded' => false,
711 'last_updated' => false,
712 'added' => false,
713 'tags' => false,
714 'homepage' => false,
715 'donate_link' => false,
716 'author_profile' => false,
717 'author' => false,
718 ),
719 )
720 );
721
722 if (is_wp_error($plugin_information)) {
723 throw new \Exception($plugin_information->get_error_message());
724 }
725
726 $package = $plugin_information->download_link;
727 $download = $upgrader->download_package($package);
728
729 if (is_wp_error($download)) {
730 throw new \Exception($download->get_error_message());
731 }
732
733 $working_dir = $upgrader->unpack_package($download, true);
734
735 if (is_wp_error($working_dir)) {
736 throw new \Exception($working_dir->get_error_message());
737 }
738
739 $result = $upgrader->install_package(
740 array(
741 'source' => $working_dir,
742 'destination' => WP_PLUGIN_DIR,
743 'clear_destination' => false,
744 'abort_if_destination_exists' => false,
745 'clear_working' => true,
746 'hook_extra' => array(
747 'type' => 'plugin',
748 'action' => 'install',
749 ),
750 )
751 );
752
753 if (is_wp_error($result)) {
754 throw new \Exception($result->get_error_message());
755 }
756
757 $activate = true;
758
759 } catch (\Exception $e) {
760 }
761
762 // Discard feedback.
763 ob_end_clean();
764 }
765
766 wp_clean_plugins_cache();
767
768 // Activate this thing.
769 if ($activate) {
770 try {
771 $result = activate_plugin($installed ? $installed_plugins[$plugin_file] : $plugin_slug . '/' . $plugin_file);
772
773 if (is_wp_error($result)) {
774 throw new \Exception($result->get_error_message());
775 }
776 } catch (\Exception $e) {
777 }
778 }
779 }
780 }
781
782 private function associate_plugin_file($plugins, $key)
783 {
784 $path = explode('/', $key);
785 $filename = end($path);
786 $plugins[$filename] = $key;
787 return $plugins;
788 }
789
790 public function getBoards(Request $request)
791 {
792 $boards = Board::select(['id', 'title', 'type'])
793 ->byAccessUser(get_current_user_id())
794 ->orderBy('title', 'ASC')
795 ->get();
796
797 return $this->sendSuccess([
798 'boards' => $boards
799 ]);
800 }
801
802 public function getPages(Request $request)
803 {
804
805 $db = App::getInstance('db');
806
807 $allPages = $db->table('posts')->where('post_type', 'page')
808 ->where('post_status', 'publish')
809 ->select(['ID', 'post_title'])
810 ->orderBy('post_title', 'ASC')
811 ->get();
812
813 $pages = [];
814 foreach ($allPages as $page) {
815 $pages[] = [
816 'id' => $page->ID,
817 'title' => $page->post_title ? $page->post_title : __('(no title)', 'fluent-boards')
818 ];
819 }
820
821 return $this->sendSuccess([
822 'pages' => $pages
823 ]);
824 }
825
826 }
827