| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBooking\App\Http\Controllers; |
| 4 |
|
| 5 |
use FluentBooking\Framework\Support\Arr; |
| 6 |
use FluentBooking\App\Services\Integrations\IntegrationManagerHelper; |
| 7 |
|
| 8 |
abstract class IntegrationManagerController extends IntegrationManagerHelper |
| 9 |
{ |
| 10 |
protected $app = null; |
| 11 |
|
| 12 |
protected $subscriber = null; |
| 13 |
|
| 14 |
protected $title = ''; |
| 15 |
|
| 16 |
protected $description = ''; |
| 17 |
|
| 18 |
protected $integrationKey = ''; |
| 19 |
|
| 20 |
protected $optionKey = ''; |
| 21 |
|
| 22 |
protected $settingsKey = ''; |
| 23 |
|
| 24 |
protected $priority = 11; |
| 25 |
|
| 26 |
public $logo = ''; |
| 27 |
|
| 28 |
public $hasGlobalMenu = false; |
| 29 |
|
| 30 |
public $category = 'crm'; |
| 31 |
|
| 32 |
public $disableGlobalSettings = 'yes'; |
| 33 |
|
| 34 |
public function __construct($title, $integrationKey, $optionKey, $settingsKey, $priority = 11) |
| 35 |
{ |
| 36 |
$this->title = $title; |
| 37 |
$this->integrationKey = $integrationKey; |
| 38 |
$this->optionKey = $optionKey; |
| 39 |
$this->settingsKey = $settingsKey; |
| 40 |
$this->priority = $priority; |
| 41 |
|
| 42 |
parent::__construct( |
| 43 |
$this->settingsKey, false, true |
| 44 |
); |
| 45 |
} |
| 46 |
|
| 47 |
public function registerAdminHooks() |
| 48 |
{ |
| 49 |
$isEnabled = $this->isEnabled(); |
| 50 |
add_filter('fluent_booking/global_addons', function ($addons) use ($isEnabled) { |
| 51 |
$addons[$this->integrationKey] = [ |
| 52 |
'title' => $this->title, |
| 53 |
'category' => $this->category, |
| 54 |
'disable_global_settings' => $this->disableGlobalSettings, |
| 55 |
'description' => $this->description, |
| 56 |
'config_url' => ('yes' != $this->disableGlobalSettings) ? admin_url('admin.php?page=fluent-booking#/settings/general-settings') : '', |
| 57 |
'logo' => $this->logo, |
| 58 |
'enabled' => ($isEnabled) ? 'yes' : 'no', |
| 59 |
]; |
| 60 |
return $addons; |
| 61 |
}, $this->priority, 1); |
| 62 |
|
| 63 |
if (!$isEnabled) { |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
$this->registerNotificationHooks(); |
| 68 |
|
| 69 |
// Global Settings Here |
| 70 |
|
| 71 |
if ($this->hasGlobalMenu) { |
| 72 |
add_filter('fluent_booking/global_settings_components', [$this, 'addGlobalMenu']); |
| 73 |
add_filter('fluent_booking/global_integration_settings_' . $this->integrationKey, [$this, 'getGlobalSettings'], |
| 74 |
$this->priority, 1); |
| 75 |
add_filter('fluent_booking/global_integration_fields_' . $this->integrationKey, [$this, 'getGlobalFields'], |
| 76 |
$this->priority, 1); |
| 77 |
add_action('fluent_booking/save_global_integration_settings_' . $this->integrationKey, |
| 78 |
[$this, 'saveGlobalSettings'], $this->priority, 1); |
| 79 |
} |
| 80 |
|
| 81 |
add_filter('fluent_booking/global_notification_types', [$this, 'addNotificationType'], $this->priority); |
| 82 |
|
| 83 |
add_filter('fluent_booking/get_available_form_integrations', [$this, 'pushIntegration'], $this->priority, 2); |
| 84 |
|
| 85 |
add_filter('fluent_booking/global_notification_feed_' . $this->settingsKey, [$this, 'setFeedAttributes'], 10, 2); |
| 86 |
|
| 87 |
add_filter('fluent_booking/get_integration_defaults_' . $this->integrationKey, [$this, 'getIntegrationDefaults'], 10, 2); |
| 88 |
add_filter('fluent_booking/get_integration_settings_fields_' . $this->integrationKey, [$this, 'getSettingsFields'], 10, 2); |
| 89 |
add_filter('fluent_booking/get_integration_merge_fields_' . $this->integrationKey, [$this, 'getMergeFields'], 10, 3); |
| 90 |
add_filter('fluent_booking/get_integration_config_field_options_' . $this->integrationKey, [$this, 'getConfigFieldOptions'], 10, 2); |
| 91 |
add_filter('fluent_booking/get_integration_values_' . $this->integrationKey, [$this, 'prepareIntegrationFeed'], 10, 3); |
| 92 |
|
| 93 |
add_filter('fluent_booking/save_integration_settings_' . $this->integrationKey, [$this, 'setMetaKey'], 10, 2); |
| 94 |
} |
| 95 |
|
| 96 |
public function registerNotificationHooks() |
| 97 |
{ |
| 98 |
if ($this->isConfigured()) { |
| 99 |
add_filter('fluent_booking/global_notification_active_types', [$this, 'addActiveNotificationType'], $this->priority); |
| 100 |
add_action('fluent_booking/integration_notify_' . $this->settingsKey, [$this, 'notify'], $this->priority, 3); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
public function notify($feed, $booking, $calendarEvent) |
| 105 |
{ |
| 106 |
// Do something here in your integration class |
| 107 |
} |
| 108 |
|
| 109 |
public function addGlobalMenu($setting) |
| 110 |
{ |
| 111 |
$setting[$this->integrationKey] = [ |
| 112 |
'hash' => 'general-' . $this->integrationKey . '-settings', |
| 113 |
'component' => 'general-integration-settings', |
| 114 |
'settings_key' => $this->integrationKey, |
| 115 |
'title' => $this->title, |
| 116 |
]; |
| 117 |
|
| 118 |
return $setting; |
| 119 |
} |
| 120 |
|
| 121 |
public function addNotificationType($types) |
| 122 |
{ |
| 123 |
$types[] = $this->settingsKey; |
| 124 |
|
| 125 |
return $types; |
| 126 |
} |
| 127 |
|
| 128 |
public function addActiveNotificationType($types) |
| 129 |
{ |
| 130 |
$types[$this->settingsKey] = $this->integrationKey; |
| 131 |
|
| 132 |
return $types; |
| 133 |
} |
| 134 |
|
| 135 |
public function getGlobalSettings($settings) |
| 136 |
{ |
| 137 |
return $settings; |
| 138 |
} |
| 139 |
|
| 140 |
public function saveGlobalSettings($settings) |
| 141 |
{ |
| 142 |
return $settings; |
| 143 |
} |
| 144 |
|
| 145 |
public function getGlobalFields($fields) |
| 146 |
{ |
| 147 |
return $fields; |
| 148 |
} |
| 149 |
|
| 150 |
public function setMetaKey($data) |
| 151 |
{ |
| 152 |
// $data['meta_key'] = $this->settingsKey; |
| 153 |
return $data; |
| 154 |
} |
| 155 |
|
| 156 |
public function prepareIntegrationFeed($setting, $feed, $calendarEventId) |
| 157 |
{ |
| 158 |
$defaults = $this->getIntegrationDefaults([], $calendarEventId); |
| 159 |
|
| 160 |
foreach ($setting as $settingKey => $settingValue) { |
| 161 |
if ('true' == $settingValue) { |
| 162 |
$setting[$settingKey] = true; |
| 163 |
} elseif ('false' == $settingValue) { |
| 164 |
$setting[$settingKey] = false; |
| 165 |
} elseif ('conditionals' == $settingKey) { |
| 166 |
if ('true' == $settingValue['status']) { |
| 167 |
$settingValue['status'] = true; |
| 168 |
} elseif ('false' == $settingValue['status']) { |
| 169 |
$settingValue['status'] = false; |
| 170 |
} |
| 171 |
$setting['conditionals'] = $settingValue; |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
if (!empty($setting['list_id'])) { |
| 176 |
$setting['list_id'] = (string)$setting['list_id']; |
| 177 |
} |
| 178 |
|
| 179 |
return wp_parse_args($setting, $defaults); |
| 180 |
} |
| 181 |
|
| 182 |
abstract public function getIntegrationDefaults($settings, $calendarEventId); |
| 183 |
|
| 184 |
abstract public function pushIntegration($integrations, $calendarEventId); |
| 185 |
|
| 186 |
abstract public function getSettingsFields($settings, $calendarEventId); |
| 187 |
|
| 188 |
abstract public function getMergeFields($list, $listId, $calendarEventId); |
| 189 |
|
| 190 |
public function getConfigFieldOptions($settings, $calendarEventId) |
| 191 |
{ |
| 192 |
return []; |
| 193 |
} |
| 194 |
|
| 195 |
public function setFeedAttributes($feed, $calendarEventId) |
| 196 |
{ |
| 197 |
$feed['provider'] = $this->integrationKey; |
| 198 |
$feed['provider_logo'] = $this->logo; |
| 199 |
|
| 200 |
return $feed; |
| 201 |
} |
| 202 |
|
| 203 |
public function isConfigured() |
| 204 |
{ |
| 205 |
$globalStatus = $this->getApiSettings(); |
| 206 |
return $globalStatus && $globalStatus['status']; |
| 207 |
} |
| 208 |
|
| 209 |
public function isEnabled() |
| 210 |
{ |
| 211 |
return true; |
| 212 |
} |
| 213 |
|
| 214 |
public function getApiSettings() |
| 215 |
{ |
| 216 |
$settings = get_option($this->optionKey); |
| 217 |
if (!$settings || empty($settings['status'])) { |
| 218 |
$settings = [ |
| 219 |
'apiKey' => '', |
| 220 |
'status' => true, |
| 221 |
]; |
| 222 |
} |
| 223 |
|
| 224 |
return $settings; |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
protected function addLog($title, $description, $bookingId, $status = 'info', $type = 'log') |
| 229 |
{ |
| 230 |
do_action('fluent_booking/log_booking_note', [ |
| 231 |
'title' => $title, |
| 232 |
'description' => $description, |
| 233 |
'booking_id' => $bookingId, |
| 234 |
'status' => $status, |
| 235 |
'type' => $type, |
| 236 |
]); |
| 237 |
} |
| 238 |
} |
| 239 |
|