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