| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBooking\App\Services\Integrations\Elementor; |
| 4 |
|
| 5 |
|
| 6 |
use FluentBooking\App\Models\Calendar; |
| 7 |
use FluentBooking\App\Models\CalendarSlot; |
| 8 |
use FluentBooking\App\App; |
| 9 |
|
| 10 |
class ElementorIntegration |
| 11 |
{ |
| 12 |
public function register() |
| 13 |
{ |
| 14 |
add_action('elementor/init', [$this, 'addElementorCategory']); |
| 15 |
|
| 16 |
add_action('elementor/widgets/widgets_registered', [$this, 'registerWidget']); |
| 17 |
add_action('elementor/controls/controls_registered', [$this, 'registerControl']); |
| 18 |
|
| 19 |
add_action('elementor/editor/before_enqueue_scripts', [$this, 'editorScripts']); |
| 20 |
add_action('elementor/editor/after_enqueue_scripts', [$this, 'editorScripts']); |
| 21 |
|
| 22 |
add_action('wp_ajax_get_calendar_events', [$this, 'ajaxGetCalendarEvents']); |
| 23 |
add_action('wp_ajax_nopriv_get_calendar_events', [$this, 'ajaxGetCalendarEvents']); |
| 24 |
|
| 25 |
add_action('wp_ajax_get_event_hash', [$this, 'ajaxGetEventHash']); |
| 26 |
add_action('wp_ajax_nopriv_get_event_hash', [$this, 'ajaxGetEventHash']); |
| 27 |
} |
| 28 |
|
| 29 |
public function editorScripts() |
| 30 |
{ |
| 31 |
wp_enqueue_script('fcal-custom-elementor', plugin_dir_url(__FILE__) . 'fcal-custom-elementor.js', ['jquery'], time(), true); |
| 32 |
|
| 33 |
wp_localize_script('fcal-custom-elementor', 'fcal_elementor_ajax_object', array( |
| 34 |
'nonce' => wp_create_nonce('calendar_events_nonce'), |
| 35 |
'ajaxurl' => admin_url('admin-ajax.php'), |
| 36 |
'svgIcon' => App::getInstance('url.assets') . 'images/fluentbooking.svg' |
| 37 |
)); |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
private function getCalendarEvents($selectedCalId = '') |
| 42 |
{ |
| 43 |
if (empty($selectedCalId)) { |
| 44 |
return []; |
| 45 |
} |
| 46 |
|
| 47 |
$calendar = Calendar::with(['events' => function ($query) { |
| 48 |
$query->where('status', 'active'); |
| 49 |
}])->find($selectedCalId); |
| 50 |
|
| 51 |
if (!$calendar) { |
| 52 |
return []; |
| 53 |
} |
| 54 |
|
| 55 |
$formattedData = []; |
| 56 |
|
| 57 |
foreach ($calendar->events as $event) { |
| 58 |
$formattedData[$event->id] = $event->title; |
| 59 |
} |
| 60 |
|
| 61 |
return $formattedData; |
| 62 |
} |
| 63 |
|
| 64 |
public function addElementorCategory() |
| 65 |
{ |
| 66 |
\Elementor\Plugin::instance()->elements_manager->add_category('fluentbooking', [ |
| 67 |
'title' => __('FluentBooking', 'fluent-booking-pro'), |
| 68 |
], 1); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* @throws \Exception |
| 73 |
*/ |
| 74 |
public function registerWidget() |
| 75 |
{ |
| 76 |
$this->includeWidgets(); |
| 77 |
$this->registerWidgets(); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* @throws \Exception |
| 82 |
*/ |
| 83 |
public function includeWidgets() |
| 84 |
{ |
| 85 |
$this->loadFile('/Widgets/FcalCalendarEvent.php'); |
| 86 |
$this->loadFile('/Widgets/FcalBookings.php'); |
| 87 |
$this->loadFile('/Widgets/FcalCalendar.php'); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* @throws \Exception |
| 92 |
*/ |
| 93 |
public function registerControl($controls_manager) |
| 94 |
{ |
| 95 |
$this->loadFile('/Controls/FluentBookingCustomGroupSelect.php'); |
| 96 |
$this->registerFluentBookingCustomGroupSelect($controls_manager); |
| 97 |
} |
| 98 |
|
| 99 |
public function ajaxGetCalendarEvents() { |
| 100 |
if (!isset($_POST['security']) || !wp_verify_nonce($_POST['security'], 'calendar_events_nonce')) { |
| 101 |
wp_send_json_error(['message' => __('Nonce verification failed', 'fluent-booking-pro')]); |
| 102 |
exit; |
| 103 |
} |
| 104 |
|
| 105 |
if (!isset($_POST['cal_id'])) { |
| 106 |
wp_send_json_error(['message' => __('No calendar ID provided', 'fluent-booking-pro')]); |
| 107 |
} |
| 108 |
|
| 109 |
$calId = intval($_POST['cal_id']); |
| 110 |
$events = []; // Fetch events using your getCalendarEvents method or similar |
| 111 |
|
| 112 |
// Example of fetching events (you need to replace this with your actual method) |
| 113 |
$events = $this->getCalendarEvents($calId); |
| 114 |
|
| 115 |
wp_send_json_success($events); |
| 116 |
} |
| 117 |
|
| 118 |
public function ajaxGetEventHash() { |
| 119 |
if (!isset($_POST['security']) || !wp_verify_nonce($_POST['security'], 'calendar_events_nonce')) { |
| 120 |
wp_send_json_error(['message' => __('Nonce verification failed', 'fluent-booking-pro')]); |
| 121 |
exit; |
| 122 |
} |
| 123 |
|
| 124 |
$eventId = intval($_POST['event_id']); |
| 125 |
|
| 126 |
$event = CalendarSlot::find($eventId); |
| 127 |
|
| 128 |
if (!$event) { |
| 129 |
wp_send_json_error(['message' => __('Event not found', 'fluent-booking-pro')]); |
| 130 |
exit; |
| 131 |
} |
| 132 |
|
| 133 |
$eventHash = $event->hash; |
| 134 |
|
| 135 |
wp_send_json_success(['hash' => $eventHash]); |
| 136 |
} |
| 137 |
|
| 138 |
public function registerWidgets() |
| 139 |
{ |
| 140 |
\Elementor\Plugin::instance()->widgets_manager->register(new \FluentBooking\App\Services\Integrations\Elementor\Widgets\FcalCalendarEvent()); |
| 141 |
\Elementor\Plugin::instance()->widgets_manager->register(new \FluentBooking\App\Services\Integrations\Elementor\Widgets\FcalBookings()); |
| 142 |
\Elementor\Plugin::instance()->widgets_manager->register(new \FluentBooking\App\Services\Integrations\Elementor\Widgets\FcalCalendar()); |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* @throws \Exception |
| 147 |
*/ |
| 148 |
private function loadFile($relativePath) |
| 149 |
{ |
| 150 |
$filePath = __DIR__ . $relativePath; |
| 151 |
if (file_exists($filePath)) { |
| 152 |
require_once($filePath); |
| 153 |
} else { |
| 154 |
throw new \Exception(sprintf(__("File not found: %s", "fluent-booking-pro"), $filePath)); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
private function registerFluentBookingCustomGroupSelect($controls_manager) |
| 159 |
{ |
| 160 |
$control = new \FluentBookingCustomGroupSelect(); |
| 161 |
\Elementor\Plugin::instance()->controls_manager->register($control, 'fcal_group_select'); |
| 162 |
} |
| 163 |
|
| 164 |
} |
| 165 |
|