| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBooking\App\Hooks\Handlers; |
| 4 |
|
| 5 |
use FluentBooking\App\App; |
| 6 |
use FluentBooking\App\Services\CalendarService; |
| 7 |
use FluentBooking\App\Services\PermissionManager; |
| 8 |
|
| 9 |
class DataImporter |
| 10 |
{ |
| 11 |
public function importCalendar() |
| 12 |
{ |
| 13 |
if (!$this->verifyNonce()) { |
| 14 |
wp_send_json_error([ |
| 15 |
'message' => __('Security check failed. Please refresh and try again.', 'fluent-booking'), |
| 16 |
]); |
| 17 |
} |
| 18 |
|
| 19 |
if (!PermissionManager::userCan(['invite_team_members', 'manage_all_data', 'manage_other_calendars'])) { |
| 20 |
wp_send_json_error([ |
| 21 |
'message' => __('You are not authorized to import calendar', 'fluent-booking'), |
| 22 |
]); |
| 23 |
} |
| 24 |
|
| 25 |
$app = App::getInstance(); |
| 26 |
|
| 27 |
$data = $app->request->all(); |
| 28 |
|
| 29 |
if (empty($data['type']) || empty($data['user_id']) || empty($data['author_timezone'])) { |
| 30 |
wp_send_json_error([ |
| 31 |
'message' => __('Please provide all required data', 'fluent-booking'), |
| 32 |
]); |
| 33 |
} |
| 34 |
|
| 35 |
$file = $app->request->file('file'); |
| 36 |
|
| 37 |
if (empty($file) || $file->getClientOriginalExtension() != 'json') { |
| 38 |
wp_send_json_error([ |
| 39 |
'message' => __('Invalid file. Please provide a valid JSON file', 'fluent-booking'), |
| 40 |
]); |
| 41 |
} |
| 42 |
|
| 43 |
$fileContent = $file->getContents(); |
| 44 |
|
| 45 |
$calendarData = wp_parse_args($data, json_decode($fileContent, true)); |
| 46 |
|
| 47 |
if (empty($calendarData)) { |
| 48 |
wp_send_json_error([ |
| 49 |
'message' => __('Invalid file. Please provide a valid JSON file', 'fluent-booking'), |
| 50 |
]); |
| 51 |
} |
| 52 |
|
| 53 |
$calendar = CalendarService::createCalendar($calendarData, false, true); |
| 54 |
|
| 55 |
if (is_wp_error($calendar)) { |
| 56 |
wp_send_json_error([ |
| 57 |
'message' => $calendar->get_error_message(), |
| 58 |
]); |
| 59 |
} |
| 60 |
|
| 61 |
wp_send_json([ |
| 62 |
'success' => true, |
| 63 |
'message' => __('Calendar imported successfully', 'fluent-booking'), |
| 64 |
]); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Verify the request nonce for AJAX actions. |
| 69 |
* |
| 70 |
* @return bool |
| 71 |
*/ |
| 72 |
private function verifyNonce() |
| 73 |
{ |
| 74 |
$nonce = isset($_REQUEST['nonce']) ? sanitize_text_field(wp_unslash($_REQUEST['nonce'])) : ''; |
| 75 |
|
| 76 |
return !empty($nonce) && wp_verify_nonce($nonce, 'fluent-booking'); |
| 77 |
} |
| 78 |
} |
| 79 |
|