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