PluginProbe
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution / 2.2.0
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution v2.2.0
2.4.0 2.3.0 2.2.5 2.2.0 2.1.2 2.1.1 trunk 1.10.0 1.10.01 1.10.02 1.5.0 1.5.01 1.5.02 1.5.1 1.5.10 1.5.20 1.5.21 1.5.22 1.5.23 1.5.24 1.5.25 1.6.0 1.7.0 1.7.1 1.7.2 All 33 releases
fluent-booking / app / Services / ImportService.php

ImportService.php in Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution 2.2.0, at app/Services/ImportService.php

71 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentBooking\App\Services;
4
5 use FluentBooking\App\Services\PermissionManager;
6
7 class ImportService
8 {
9 /*
10 * Import host data from JSON
11 * @param array|string $data JSON data or array
12 * @param bool $useCurrentUser If true, the current user will be used as the host
13 * @return \FluentBooking\App\Models\Calendar|\WP_Error
14 */
15 public function importHostJson($data, $useCurrentUser = true)
16 {
17 if (!PermissionManager::userCan(['manage_all_data', 'invite_team_members', 'manage_other_calendars'])) {
18 return new \WP_Error('invalid_data', __('You are not authorized to import calendar', 'fluent-booking'));
19 }
20
21 if (is_string($data)) {
22 $data = json_decode($data, true);
23 }
24
25 if (!$data || !is_array($data) || empty($data['data_type']) || $data['data_type'] !== 'host') {
26 return new \WP_Error('invalid_data', 'Invalid data provided');
27 }
28
29 if (empty($data['title']) || empty($data['slug'])) {
30 return new \WP_Error('invalid_data', 'Invalid data provided');
31 }
32
33 $createdCalendar = CalendarService::createCalendar($data, $useCurrentUser);
34
35 if (is_wp_error($createdCalendar)) {
36 return new \WP_Error($createdCalendar->get_error_code(), $createdCalendar->get_error_message());
37 }
38
39 return $createdCalendar;
40 }
41
42 /*
43 * Import host data from JSON URL
44 * @param string $jsonUrl JSON URL
45 * @param bool $useCurrentUser If true, the current user will be used as the host
46 * @return \FluentBooking\App\Models\Calendar|\WP_Error
47 */
48 public function importHostByJSONUrl($jsonUrl, $useCurrentUser = true)
49 {
50 $response = wp_safe_remote_get($jsonUrl, [
51 'timeout' => 30,
52 'headers' => [
53 'Accept' => 'application/json'
54 ]
55 ]);
56
57 if (is_wp_error($response)) {
58 return $response;
59 }
60
61 // check if the status code is not 200
62 if (wp_remote_retrieve_response_code($response) !== 200) {
63 return new \WP_Error('invalid_response', 'Invalid response from the server');
64 }
65
66 $body = json_decode(wp_remote_retrieve_body($response), true);
67
68 return $this->importHostJson($body, $useCurrentUser);
69 }
70 }
71