PluginProbe
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration / 1.32
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration v1.32
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 / Services / Helper.php

Helper.php in FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration 1.32, at app/Services/Helper.php

464 lines 13.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\Services;
4
5 use FluentBoards\App\Models\Activity;
6 use FluentBoards\App\Models\Stage;
7 use FluentBoards\App\Models\Webhook;
8 use FluentBoards\Framework\Support\Arr;
9 use FluentBoards\Framework\Support\Str;
10 use FluentBoards\App\Models\Board;
11 use FluentBoards\App\Services\Parsedown;
12 use FluentBoards\App\Services\PermissionManager;
13
14 class Helper
15 {
16 public static function snake_case($string)
17 {
18 return strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $string));
19 }
20
21 public static function slugify($text, $id = '', $length = 20)
22 {
23 $text = substr($text, 0, $length); // limiting to max 20 chars
24 $text = Str::slug($text);
25 if ($id) {
26 $text = $id . '-' . $text;
27 }
28
29 return $text;
30 }
31
32 public static function loadView($template, $data)
33 {
34 extract($data, EXTR_OVERWRITE);
35
36 $template = sanitize_file_name($template);
37
38 $template = str_replace('.', DIRECTORY_SEPARATOR, $template);
39
40 ob_start();
41 include FLUENT_BOARDS_PLUGIN_PATH . 'app/Views/' . $template . '.php';
42
43 return ob_get_clean();
44 }
45
46 private static function sanitizeData($data, $fieldMaps)
47 {
48 foreach ($data as $key => $value) {
49 if ($value && isset($fieldMaps[$key]) && !is_array($value)) {
50 $data[$key] = call_user_func($fieldMaps[$key], $value);
51 }
52 }
53
54 return $data;
55 }
56
57 public static function sanitizeTask($data)
58 {
59 $fieldMaps = [
60 'title' => 'sanitize_text_field',
61 'board_id' => 'intval',
62 'parent_id' => 'intval',
63 'crm_contact_id' => 'intval',
64 'task_type' => 'sanitize_text_field',
65 'stage' => 'sanitize_text_field',
66 'reminder_type' => 'sanitize_text_field',
67 'priority' => 'sanitize_text_field',
68 'lead_value' => 'doubleval',
69 'remind_at' => 'sanitize_text_field',
70 'scope' => 'sanitize_text_field',
71 'source' => 'sanitize_text_field',
72 'description' => 'wp_kses_post',
73 'due_date' => 'sanitize_text_field',
74 'start_at' => 'sanitize_text_field',
75 'log_minutes' => 'sanitize_text_field',
76 'last_completed' => 'sanitize_text_field',
77 'assignees' => 'intval',
78 'is_archived' => 'intval',
79 'previous_stage' => 'sanitize_text_field',
80 'new_stage' => 'sanitize_text_field',
81 'new_index' => 'intval',
82 'old_index' => 'intval',
83 'new_board_id' => 'intval',
84 'position' => 'intval'
85
86 ];
87
88 return self::sanitizeData($data, $fieldMaps);
89 }
90
91 public static function sanitizeBoard($data)
92 {
93 $fieldMaps = [
94 'board_id' => 'intval',
95 'title' => 'sanitize_text_field',
96 'parent_id' => 'intval',
97 'type' => 'sanitize_text_field',
98 'description' => 'wp_kses_post',
99 'currency' => 'sanitize_text_field',
100 'image_url' => 'sanitize_url',
101 'is_auth_require' => 'intval',
102 'crm_contact_id' => 'intval',
103 'id' => 'sanitize_text_field',
104 'is_image' => 'rest_sanitize_boolean',
105 'color' => 'sanitize_text_field', // sanitize_hex_color doesn't work when color code is greater than 6 characters
106 ];
107
108 return self::sanitizeData($data, $fieldMaps);
109 }
110
111 public static function sanitizeStage($data)
112 {
113 $fieldMaps = [
114 'title' => 'sanitize_text_field',
115 'slug' => 'sanitize_text_field',
116 'type' => 'sanitize_text_field',
117 'status' => 'sanitize_text_field',
118 ];
119
120 return self::sanitizeData($data, $fieldMaps);
121 }
122
123 public static function sanitizeComment($data)
124 {
125 $fieldMaps = [
126 'description' => 'wp_kses_post',
127 'created_by' => 'intval',
128 'task_id' => 'intval',
129 'type' => 'sanitize_text_field',
130 ];
131
132 return self::sanitizeData($data, $fieldMaps);
133 }
134
135 public static function sanitizeLabel($data)
136 {
137 $fieldMaps = [
138 'bg_color' => 'sanitize_hex_color',
139 'color' => 'sanitize_hex_color',
140 'label' => 'sanitize_text_field',
141 'boardId' => 'intval',
142 'task_id' => 'intval',
143 'meta_value' => 'intval',
144 ];
145
146 return self::sanitizeData($data, $fieldMaps);
147 }
148
149 public static function sanitizeSubtask($data)
150 {
151 $fieldMaps = [
152 'title' => 'sanitize_text_field',
153 'stage' => 'sanitize_text_field',
154 'newPosition' => 'intval',
155 'priority' => 'sanitize_text_field',
156 'task_type' => 'sanitize_text_field',
157 'board_id' => 'intval',
158 'created_by' => 'intval',
159 'due_date' => 'sanitize_text_field',
160 ];
161
162 return self::sanitizeData($data, $fieldMaps);
163 }
164
165 public static function createActivity($data)
166 {
167 return Activity::create($data);
168 }
169
170 public static function sanitizeTaskMeta($data)
171 {
172 $fieldMaps = [
173 'title' => 'sanitize_text_field',
174 'url' => 'sanitize_url',
175
176 ];
177
178 return self::sanitizeData($data, $fieldMaps);
179 }
180
181 public static function getFormattedStagesByBoardId($boardId)
182 {
183 if (!$boardId) {
184 return [];
185 }
186
187 $board = Board::findOrFail($boardId);
188
189 $stages = $board->stages()->get();
190 // return static::formateStage($stages);
191 return $stages;
192 }
193
194 public static function getStagesByBoardId($boardId)
195 {
196 if (!$boardId) {
197 return [];
198 }
199
200 $board = Board::findOrFail($boardId);
201
202 $stages = $board->stages()->get();
203 return static::formateStage($stages);
204 // return $stages;
205 }
206
207 public static function formateStage($stages)
208 {
209 if (!$stages) return [];
210
211 $formattedStages = [];
212 foreach ($stages as $stage) {
213 $boardName = $stage->board->title;
214 $formattedStages[] = [
215 'id' => strval($stage->id),
216 'title' => $boardName . ' - ' . $stage->title
217 ];
218 }
219 return $formattedStages;
220 }
221
222 public static function getIdTitleArray($data)
223 {
224 $array = [];
225 foreach ($data as $item) {
226 $array[] = [
227 'id' => $item->id,
228 'title' => $item->title
229 ];
230 }
231 return $array;
232 }
233
234 // get Task Url by task id and board id
235 public static function getTaskUrl($taskId, $boardId): string
236 {
237 if (!$taskId || !$boardId) {
238 return '';
239 }
240 return fluent_boards_page_url() . 'boards/' . $boardId . '/tasks/' . $taskId;
241 }
242
243 public static function getTaskUrlByTask($task): string
244 {
245 if (!$task) {
246 return '';
247 }
248 return fluent_boards_page_url() . 'boards/' . $task->board_id . '/tasks/' . $task->id;
249 }
250
251 public static function getBoardUrl($boardId): string
252 {
253 if (!$boardId) {
254 return '';
255 }
256 return fluent_boards_page_url() . 'boards/' . $boardId;
257 }
258
259 public static function crm_contact($id)
260 {
261 if (!defined('FLUENTCRM')) {
262 return '';
263 }
264
265 $contact = \FluentCrm\App\Models\Subscriber::with(['tags', 'lists'])->find($id);
266
267 if (!$contact) {
268 return null;
269 }
270
271 return [
272 'id' => $contact->id,
273 'email' => $contact->email,
274 'first_name' => $contact->first_name,
275 'last_name' => $contact->last_name,
276 'full_name' => $contact->full_name,
277 'avatar' => $contact->avatar,
278 'photo' => $contact->photo,
279 'status' => $contact->status,
280 'contact_type' => $contact->contact_type,
281 'last_activity' => $contact->last_activity,
282 'life_time_value' => $contact->life_time_value,
283 'total_points' => $contact->total_points,
284 'user_id' => $contact->user_id,
285 'created_at' => $contact->created_at,
286 'tags' => Helper::getIdTitleArray($contact->tags),
287 'lists' => Helper::getIdTitleArray($contact->lists)
288
289 ];
290
291 }
292
293 public static function getStagesByBoardGroup()
294 {
295 $boards = self::getBoards();
296
297 $groups = [];
298 foreach ($boards as $board) {
299 $groups[] = [
300 'title' => $board->title,
301 'slug' => 'aaa' . '_' . $board->id,
302 'options' => self::getStagesByBoardId($board->id)
303 ];
304 }
305 return $groups;
306 }
307
308 public static function getBoards()
309 {
310 return Board::orderBy('created_at', 'ASC')->get();
311 }
312
313 public static function getStage($stageId)
314 {
315 return Stage::findOrFail($stageId);
316 }
317
318 public static function getBoardByStageId($stageId)
319 {
320 $stage = self::getStage($stageId);
321 return $stage->board;
322 }
323
324 public static function sanitizeUserCollections($users)
325 {
326 if (current_user_can('list_users')) {
327 return $users;
328 }
329
330 if ($users) {
331 $users->makeHidden(['user_email', 'user_nicename', 'user_registered', 'user_url', 'user_status']);
332 }
333
334 return $users;
335 }
336
337 public static function sanitizeUsersArray($users, $boardId = null)
338 {
339 if (current_user_can('list_users')) {
340 return $users;
341 }
342
343 $sanitizedUsers = [];
344
345 if(!PermissionManager::isBoardManager($boardId)) //Todo: may create permission security issue, will be modified later
346 {
347 foreach ($users as $user) {
348 unset($user->user_email);
349 unset($user->email);
350 $sanitizedUsers[] = $user;
351 }
352 } else {
353 foreach ($users as $user) {
354 $sanitizedUsers[] = $user;
355 }
356 }
357
358 return $sanitizedUsers;
359 }
360
361 public static function getPriorityOptions()
362 {
363 return [
364 [
365 'id' => 'low',
366 'title' => 'Low'
367 ],
368 [
369 'id' => 'medium',
370 'title' => 'Medium'
371 ],
372 [
373 'id' => 'high',
374 'title' => 'High'
375 ],
376 ];
377 }
378
379 public static function dueDateConversion($due_time, $unit)
380 {
381 if($due_time <= 0) {
382 return null;
383 }
384 $currentTime = current_time('mysql');
385 $readyString = '+' . $due_time . ' ' . $unit;
386 return gmdate('Y-m-d H:i:s',strtotime($readyString,strtotime($currentTime)));
387 }
388
389 public static function searchWordPressUsers($searchQuery, $limit = 20)
390 {
391 $search = sanitize_text_field($searchQuery);
392
393 // Search by user login, email, and nicename
394 $args = array(
395 'role' => '',
396 'search' => '*' . $search . '*',
397 'number' => $limit,
398 );
399
400 // Get users by login, email, and nicename
401 $user_query = new \WP_User_Query($args);
402 $users_by_login = $user_query->get_results();
403
404 // Search by first name and last name
405 $meta_query_args = array(
406 'relation' => 'OR',
407 array(
408 'key' => 'first_name',
409 'value' => $search,
410 'compare' => 'LIKE',
411 ),
412 array(
413 'key' => 'last_name',
414 'value' => $search,
415 'compare' => 'LIKE',
416 ),
417 );
418
419 $meta_query = array(
420 'role' => '',
421 'meta_query' => $meta_query_args,
422 'number' => $limit,
423 );
424
425 // Get users by first name and last name
426 $users_by_meta = get_users($meta_query);
427
428 // Merge and remove duplicates
429 $users = array_merge($users_by_login, $users_by_meta);
430 $users = array_unique($users, SORT_REGULAR);
431
432 return $users;
433
434 }
435
436 public static function sanitizeTaskAttachment($data)
437 {
438 $fieldMaps = [
439 'title' => 'sanitize_text_field',
440 'url' => 'sanitize_url',
441 ];
442
443 return self::sanitizeData($data, $fieldMaps);
444 }
445
446 public static function sanitizeTaskRepeatData($data)
447 {
448 $fieldMaps = [
449 'create_new' => 'intval',
450 'repeat_in' => 'intval',
451 'repeat_type' => 'sanitize_text_field',
452 'repeat_when_complete' => 'intval',
453 'selected_month' => 'sanitize_text_field',
454 'board_id' => 'intval',
455 'time' => 'sanitize_text_field',
456 'time_zone' => 'sanitize_text_field',
457 'next_repeat_date' => 'sanitize_text_field',
458 'repeat_in_month_type' => 'sanitize_text_field',
459 ];
460
461 return self::sanitizeData($data, $fieldMaps);
462 }
463 }
464