| 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 |
'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 |
'created_by' => 'intval', |
| 107 |
]; |
| 108 |
|
| 109 |
return self::sanitizeData($data, $fieldMaps); |
| 110 |
} |
| 111 |
|
| 112 |
public static function sanitizeStage($data) |
| 113 |
{ |
| 114 |
$fieldMaps = [ |
| 115 |
'title' => 'sanitize_text_field', |
| 116 |
'slug' => 'sanitize_text_field', |
| 117 |
'type' => 'sanitize_text_field', |
| 118 |
'status' => 'sanitize_text_field', |
| 119 |
]; |
| 120 |
|
| 121 |
return self::sanitizeData($data, $fieldMaps); |
| 122 |
} |
| 123 |
|
| 124 |
public static function sanitizeComment($data) |
| 125 |
{ |
| 126 |
$fieldMaps = [ |
| 127 |
'description' => 'wp_kses_post', |
| 128 |
'created_by' => 'intval', |
| 129 |
'task_id' => 'intval', |
| 130 |
'type' => 'sanitize_text_field', |
| 131 |
]; |
| 132 |
|
| 133 |
return self::sanitizeData($data, $fieldMaps); |
| 134 |
} |
| 135 |
|
| 136 |
public static function sanitizeLabel($data) |
| 137 |
{ |
| 138 |
$fieldMaps = [ |
| 139 |
'bg_color' => 'sanitize_hex_color', |
| 140 |
'color' => 'sanitize_hex_color', |
| 141 |
'label' => 'sanitize_text_field', |
| 142 |
'boardId' => 'intval', |
| 143 |
'task_id' => 'intval', |
| 144 |
'meta_value' => 'intval', |
| 145 |
]; |
| 146 |
|
| 147 |
return self::sanitizeData($data, $fieldMaps); |
| 148 |
} |
| 149 |
|
| 150 |
public static function sanitizeSubtask($data) |
| 151 |
{ |
| 152 |
$fieldMaps = [ |
| 153 |
'title' => 'sanitize_text_field', |
| 154 |
'stage' => 'sanitize_text_field', |
| 155 |
'newPosition' => 'intval', |
| 156 |
'priority' => 'sanitize_text_field', |
| 157 |
'type' => 'sanitize_text_field', |
| 158 |
'board_id' => 'intval', |
| 159 |
'created_by' => 'intval', |
| 160 |
'due_date' => 'sanitize_text_field', |
| 161 |
]; |
| 162 |
|
| 163 |
return self::sanitizeData($data, $fieldMaps); |
| 164 |
} |
| 165 |
|
| 166 |
public static function createActivity($data) |
| 167 |
{ |
| 168 |
return Activity::create($data); |
| 169 |
} |
| 170 |
|
| 171 |
public static function sanitizeTaskMeta($data) |
| 172 |
{ |
| 173 |
$fieldMaps = [ |
| 174 |
'title' => 'sanitize_text_field', |
| 175 |
'url' => 'sanitize_url', |
| 176 |
|
| 177 |
]; |
| 178 |
|
| 179 |
return self::sanitizeData($data, $fieldMaps); |
| 180 |
} |
| 181 |
|
| 182 |
public static function getFormattedStagesByBoardId($boardId) |
| 183 |
{ |
| 184 |
if (!$boardId) { |
| 185 |
return []; |
| 186 |
} |
| 187 |
|
| 188 |
$board = Board::findOrFail($boardId); |
| 189 |
|
| 190 |
$stages = $board->stages()->get(); |
| 191 |
// return static::formateStage($stages); |
| 192 |
return $stages; |
| 193 |
} |
| 194 |
|
| 195 |
public static function getStagesByBoardId($boardId) |
| 196 |
{ |
| 197 |
if (!$boardId) { |
| 198 |
return []; |
| 199 |
} |
| 200 |
|
| 201 |
$board = Board::findOrFail($boardId); |
| 202 |
|
| 203 |
$stages = $board->stages()->get(); |
| 204 |
return static::formateStage($stages); |
| 205 |
// return $stages; |
| 206 |
} |
| 207 |
|
| 208 |
public static function formateStage($stages) |
| 209 |
{ |
| 210 |
if (!$stages) return []; |
| 211 |
|
| 212 |
$formattedStages = []; |
| 213 |
foreach ($stages as $stage) { |
| 214 |
$boardName = $stage->board->title; |
| 215 |
$formattedStages[] = [ |
| 216 |
'id' => strval($stage->id), |
| 217 |
'title' => $boardName . ' - ' . $stage->title |
| 218 |
]; |
| 219 |
} |
| 220 |
return $formattedStages; |
| 221 |
} |
| 222 |
|
| 223 |
public static function getIdTitleArray($data) |
| 224 |
{ |
| 225 |
$array = []; |
| 226 |
foreach ($data as $item) { |
| 227 |
$array[] = [ |
| 228 |
'id' => $item->id, |
| 229 |
'title' => $item->title |
| 230 |
]; |
| 231 |
} |
| 232 |
return $array; |
| 233 |
} |
| 234 |
|
| 235 |
// get Task Url by task id and board id |
| 236 |
public static function getTaskUrl($taskId, $boardId): string |
| 237 |
{ |
| 238 |
if (!$taskId || !$boardId) { |
| 239 |
return ''; |
| 240 |
} |
| 241 |
return fluent_boards_page_url() . 'boards/' . $boardId . '/tasks/' . $taskId; |
| 242 |
} |
| 243 |
|
| 244 |
public static function getTaskUrlByTask($task): string |
| 245 |
{ |
| 246 |
if (!$task) { |
| 247 |
return ''; |
| 248 |
} |
| 249 |
return fluent_boards_page_url() . 'boards/' . $task->board_id . '/tasks/' . $task->id; |
| 250 |
} |
| 251 |
|
| 252 |
public static function getBoardUrl($boardId): string |
| 253 |
{ |
| 254 |
if (!$boardId) { |
| 255 |
return ''; |
| 256 |
} |
| 257 |
return fluent_boards_page_url() . 'boards/' . $boardId; |
| 258 |
} |
| 259 |
|
| 260 |
public static function crm_contact($id) |
| 261 |
{ |
| 262 |
if (!defined('FLUENTCRM')) { |
| 263 |
return ''; |
| 264 |
} |
| 265 |
|
| 266 |
$contact = \FluentCrm\App\Models\Subscriber::with(['tags', 'lists'])->find($id); |
| 267 |
|
| 268 |
if (!$contact) { |
| 269 |
return null; |
| 270 |
} |
| 271 |
|
| 272 |
return [ |
| 273 |
'id' => $contact->id, |
| 274 |
'email' => $contact->email, |
| 275 |
'first_name' => $contact->first_name, |
| 276 |
'last_name' => $contact->last_name, |
| 277 |
'full_name' => $contact->full_name, |
| 278 |
'avatar' => $contact->avatar, |
| 279 |
'photo' => $contact->photo, |
| 280 |
'status' => $contact->status, |
| 281 |
'contact_type' => $contact->contact_type, |
| 282 |
'last_activity' => $contact->last_activity, |
| 283 |
'life_time_value' => $contact->life_time_value, |
| 284 |
'total_points' => $contact->total_points, |
| 285 |
'user_id' => $contact->user_id, |
| 286 |
'created_at' => $contact->created_at, |
| 287 |
'tags' => Helper::getIdTitleArray($contact->tags), |
| 288 |
'lists' => Helper::getIdTitleArray($contact->lists) |
| 289 |
|
| 290 |
]; |
| 291 |
|
| 292 |
} |
| 293 |
|
| 294 |
public static function getStagesByBoardGroup() |
| 295 |
{ |
| 296 |
$boards = self::getBoards(); |
| 297 |
|
| 298 |
$groups = []; |
| 299 |
foreach ($boards as $board) { |
| 300 |
$groups[] = [ |
| 301 |
'title' => $board->title, |
| 302 |
'slug' => 'aaa' . '_' . $board->id, |
| 303 |
'options' => self::getStagesByBoardId($board->id) |
| 304 |
]; |
| 305 |
} |
| 306 |
return $groups; |
| 307 |
} |
| 308 |
|
| 309 |
public static function getBoards() |
| 310 |
{ |
| 311 |
return Board::orderBy('created_at', 'ASC')->get(); |
| 312 |
} |
| 313 |
|
| 314 |
public static function getStage($stageId) |
| 315 |
{ |
| 316 |
return Stage::findOrFail($stageId); |
| 317 |
} |
| 318 |
|
| 319 |
public static function getBoardByStageId($stageId) |
| 320 |
{ |
| 321 |
$stage = self::getStage($stageId); |
| 322 |
return $stage->board; |
| 323 |
} |
| 324 |
|
| 325 |
public static function sanitizeUserCollections($users) |
| 326 |
{ |
| 327 |
if (current_user_can('list_users')) { |
| 328 |
return $users; |
| 329 |
} |
| 330 |
|
| 331 |
if ($users) { |
| 332 |
$users->makeHidden(['user_email', 'user_nicename', 'user_registered', 'user_url', 'user_status']); |
| 333 |
} |
| 334 |
|
| 335 |
return $users; |
| 336 |
} |
| 337 |
|
| 338 |
public static function sanitizeUsersArray($users, $boardId = null) |
| 339 |
{ |
| 340 |
if (current_user_can('list_users')) { |
| 341 |
return $users; |
| 342 |
} |
| 343 |
|
| 344 |
$sanitizedUsers = []; |
| 345 |
|
| 346 |
if(!PermissionManager::isBoardManager($boardId)) //Todo: may create permission security issue, will be modified later |
| 347 |
{ |
| 348 |
foreach ($users as $user) { |
| 349 |
unset($user->user_email); |
| 350 |
unset($user->email); |
| 351 |
$sanitizedUsers[] = $user; |
| 352 |
} |
| 353 |
} else { |
| 354 |
foreach ($users as $user) { |
| 355 |
$sanitizedUsers[] = $user; |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
return $sanitizedUsers; |
| 360 |
} |
| 361 |
|
| 362 |
public static function getPriorityOptions() |
| 363 |
{ |
| 364 |
return [ |
| 365 |
[ |
| 366 |
'id' => 'low', |
| 367 |
'title' => 'Low' |
| 368 |
], |
| 369 |
[ |
| 370 |
'id' => 'medium', |
| 371 |
'title' => 'Medium' |
| 372 |
], |
| 373 |
[ |
| 374 |
'id' => 'high', |
| 375 |
'title' => 'High' |
| 376 |
], |
| 377 |
]; |
| 378 |
} |
| 379 |
|
| 380 |
public static function dueDateConversion($due_time, $unit) |
| 381 |
{ |
| 382 |
if($due_time <= 0) { |
| 383 |
return null; |
| 384 |
} |
| 385 |
$currentTime = current_time('mysql'); |
| 386 |
$readyString = '+' . $due_time . ' ' . $unit; |
| 387 |
return gmdate('Y-m-d H:i:s',strtotime($readyString,strtotime($currentTime))); |
| 388 |
} |
| 389 |
|
| 390 |
public static function searchWordPressUsers($searchQuery, $limit = 20) |
| 391 |
{ |
| 392 |
$search = sanitize_text_field($searchQuery); |
| 393 |
|
| 394 |
// Search by user login, email, and nicename |
| 395 |
$args = array( |
| 396 |
'role' => '', |
| 397 |
'search' => '*' . $search . '*', |
| 398 |
'number' => $limit, |
| 399 |
); |
| 400 |
|
| 401 |
// Get users by login, email, and nicename |
| 402 |
$user_query = new \WP_User_Query($args); |
| 403 |
$users_by_login = $user_query->get_results(); |
| 404 |
|
| 405 |
// Search by first name and last name |
| 406 |
$meta_query_args = array( |
| 407 |
'relation' => 'OR', |
| 408 |
array( |
| 409 |
'key' => 'first_name', |
| 410 |
'value' => $search, |
| 411 |
'compare' => 'LIKE', |
| 412 |
), |
| 413 |
array( |
| 414 |
'key' => 'last_name', |
| 415 |
'value' => $search, |
| 416 |
'compare' => 'LIKE', |
| 417 |
), |
| 418 |
); |
| 419 |
|
| 420 |
$meta_query = array( |
| 421 |
'role' => '', |
| 422 |
'meta_query' => $meta_query_args, |
| 423 |
'number' => $limit, |
| 424 |
); |
| 425 |
|
| 426 |
// Get users by first name and last name |
| 427 |
$users_by_meta = get_users($meta_query); |
| 428 |
|
| 429 |
// Merge and remove duplicates |
| 430 |
$users = array_merge($users_by_login, $users_by_meta); |
| 431 |
$users = array_unique($users, SORT_REGULAR); |
| 432 |
|
| 433 |
return $users; |
| 434 |
|
| 435 |
} |
| 436 |
|
| 437 |
public static function sanitizeTaskAttachment($data) |
| 438 |
{ |
| 439 |
$fieldMaps = [ |
| 440 |
'title' => 'sanitize_text_field', |
| 441 |
'url' => 'sanitize_url', |
| 442 |
]; |
| 443 |
|
| 444 |
return self::sanitizeData($data, $fieldMaps); |
| 445 |
} |
| 446 |
|
| 447 |
public static function sanitizeTaskRepeatData($data) |
| 448 |
{ |
| 449 |
$fieldMaps = [ |
| 450 |
'create_new' => 'intval', |
| 451 |
'repeat_in' => 'intval', |
| 452 |
'repeat_type' => 'sanitize_text_field', |
| 453 |
'repeat_when_complete' => 'intval', |
| 454 |
'selected_month' => 'sanitize_text_field', |
| 455 |
'board_id' => 'intval', |
| 456 |
'time' => 'sanitize_text_field', |
| 457 |
'time_zone' => 'sanitize_text_field', |
| 458 |
'next_repeat_date' => 'sanitize_text_field', |
| 459 |
'repeat_in_month_type' => 'sanitize_text_field', |
| 460 |
]; |
| 461 |
|
| 462 |
return self::sanitizeData($data, $fieldMaps); |
| 463 |
} |
| 464 |
} |
| 465 |
|