| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Services\Integrations; |
| 4 |
|
| 5 |
abstract class IntegrationManager |
| 6 |
{ |
| 7 |
protected $app = null; |
| 8 |
protected $subscriber = null; |
| 9 |
protected $title = ''; |
| 10 |
protected $description = ''; |
| 11 |
protected $integrationKey = ''; |
| 12 |
protected $optionKey = ''; |
| 13 |
protected $settingsKey = ''; |
| 14 |
protected $priority = 11; |
| 15 |
public $logo = ''; |
| 16 |
public $hasGlobalMenu = true; |
| 17 |
public $category = 'crm'; |
| 18 |
public $disableGlobalSettings = 'no'; |
| 19 |
|
| 20 |
public function __construct($app, $title, $integrationKey, $optionKey, $settingsKey, $priority = 11) |
| 21 |
{ |
| 22 |
$this->app = $app; |
| 23 |
$this->title = $title; |
| 24 |
$this->integrationKey = $integrationKey; |
| 25 |
$this->optionKey = $optionKey; |
| 26 |
$this->settingsKey = $settingsKey; |
| 27 |
$this->priority = $priority; |
| 28 |
} |
| 29 |
|
| 30 |
public function registerAdminHooks() |
| 31 |
{ |
| 32 |
$isEnabled = $this->isEnabled(); |
| 33 |
|
| 34 |
add_filter('fluentform_global_addons', function ($addons) use ($isEnabled) { |
| 35 |
$addons[$this->integrationKey] = [ |
| 36 |
'title' => $this->title, |
| 37 |
'category' => $this->category, |
| 38 |
'disable_global_settings' => $this->disableGlobalSettings, |
| 39 |
'description' => $this->description, |
| 40 |
'config_url' => ($this->disableGlobalSettings != 'yes') ? admin_url('admin.php?page=fluent_forms_settings#general-'.$this->integrationKey.'-settings') : '', |
| 41 |
'logo' => $this->logo, |
| 42 |
'enabled' => ($isEnabled) ? 'yes' : 'no' |
| 43 |
]; |
| 44 |
return $addons; |
| 45 |
}, $this->priority, 1); |
| 46 |
|
| 47 |
if(!$isEnabled) { |
| 48 |
return; |
| 49 |
} |
| 50 |
|
| 51 |
$this->registerNotificationHooks(); |
| 52 |
|
| 53 |
// Global Settings Here |
| 54 |
|
| 55 |
if ($this->hasGlobalMenu) { |
| 56 |
add_filter('fluentform_global_settings_components', array($this, 'addGlobalMenu')); |
| 57 |
add_filter('fluentform_global_integration_settings_' . $this->integrationKey, array($this, 'getGlobalSettings'), $this->priority, 1); |
| 58 |
add_filter('fluentform_global_integration_fields_' . $this->integrationKey, array($this, 'getGlobalFields'), $this->priority, 1); |
| 59 |
add_action('fluentform_save_global_integration_settings_' . $this->integrationKey, array($this, 'saveGlobalSettings'), $this->priority, 1); |
| 60 |
} |
| 61 |
|
| 62 |
add_filter('fluentform_global_notification_types', array($this, 'addNotificationType'), $this->priority); |
| 63 |
|
| 64 |
add_filter('fluentform_get_available_form_integrations', array($this, 'pushIntegration'), $this->priority, 2); |
| 65 |
|
| 66 |
add_filter('fluentform_global_notification_feed_' . $this->settingsKey, array($this, 'setFeedAtributes'), 10, 2); |
| 67 |
|
| 68 |
add_filter('fluentform_get_integration_defaults_' . $this->integrationKey, array($this, 'getIntegrationDefaults'), 10, 2); |
| 69 |
add_filter('fluentform_get_integration_settings_fields_' . $this->integrationKey, array($this, 'getSettingsFields'), 10, 2); |
| 70 |
add_filter('fluentform_get_integration_merge_fields_' . $this->integrationKey, array($this, 'getMergeFields'), 10, 3); |
| 71 |
|
| 72 |
add_filter('fluentform_save_integration_settings_' . $this->integrationKey, array($this, 'setMetaKey'), 10, 2); |
| 73 |
add_filter('fluentform_get_integration_values_' . $this->integrationKey, array($this, 'prepareIntegrationFeed'), 10, 3); |
| 74 |
} |
| 75 |
|
| 76 |
public function registerNotificationHooks() |
| 77 |
{ |
| 78 |
if ($this->isConfigured()) { |
| 79 |
add_filter('fluentform_global_notification_active_types', array($this, 'addActiveNotificationType'), $this->priority); |
| 80 |
add_action('fluentform_integration_notify_' . $this->settingsKey, array($this, 'notify'), $this->priority, 4); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
public function notify($feed, $formData, $entry, $form) |
| 85 |
{ |
| 86 |
// Each integration have to implement this notify method |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
public function addGlobalMenu($setting) |
| 91 |
{ |
| 92 |
$setting[$this->integrationKey] = [ |
| 93 |
'hash' => 'general-' . $this->integrationKey . '-settings', |
| 94 |
'component' => 'general-integration-settings', |
| 95 |
'settings_key' => $this->integrationKey, |
| 96 |
'title' => $this->title |
| 97 |
]; |
| 98 |
return $setting; |
| 99 |
} |
| 100 |
|
| 101 |
public function addNotificationType($types) |
| 102 |
{ |
| 103 |
$types[] = $this->settingsKey; |
| 104 |
return $types; |
| 105 |
} |
| 106 |
|
| 107 |
public function addActiveNotificationType($types) |
| 108 |
{ |
| 109 |
$types[$this->settingsKey] = $this->integrationKey; |
| 110 |
return $types; |
| 111 |
} |
| 112 |
|
| 113 |
public function getGlobalSettings($settings) |
| 114 |
{ |
| 115 |
return $settings; |
| 116 |
} |
| 117 |
|
| 118 |
public function saveGlobalSettings($settings) |
| 119 |
{ |
| 120 |
return $settings; |
| 121 |
} |
| 122 |
|
| 123 |
public function getGlobalFields($fields) |
| 124 |
{ |
| 125 |
return $fields; |
| 126 |
} |
| 127 |
|
| 128 |
public function setMetaKey($data, $integrationId) |
| 129 |
{ |
| 130 |
$data['meta_key'] = $this->settingsKey; |
| 131 |
return $data; |
| 132 |
} |
| 133 |
|
| 134 |
public function prepareIntegrationFeed($setting, $feed, $formId) |
| 135 |
{ |
| 136 |
$defaults = $this->getIntegrationDefaults([], $formId); |
| 137 |
|
| 138 |
foreach ($setting as $settingKey => $settingValue) { |
| 139 |
if ($settingValue == 'true') { |
| 140 |
$setting[$settingKey] = true; |
| 141 |
} else if ($settingValue == 'false') { |
| 142 |
$setting[$settingKey] = false; |
| 143 |
} else if ($settingKey == 'conditionals') { |
| 144 |
if ($settingValue['status'] == 'true') { |
| 145 |
$settingValue['status'] = true; |
| 146 |
} else if ($settingValue['status'] == 'false') { |
| 147 |
$settingValue['status'] = false; |
| 148 |
} |
| 149 |
$setting['conditionals'] = $settingValue; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
if (!empty($setting['list_id'])) { |
| 154 |
$setting['list_id'] = (string)$setting['list_id']; |
| 155 |
} |
| 156 |
|
| 157 |
return wp_parse_args($setting, $defaults); |
| 158 |
} |
| 159 |
|
| 160 |
public abstract function getIntegrationDefaults($settings, $formId); |
| 161 |
|
| 162 |
public abstract function pushIntegration($integrations, $formId); |
| 163 |
|
| 164 |
public abstract function getSettingsFields($settings, $formId); |
| 165 |
|
| 166 |
public abstract function getMergeFields($list, $listId, $formId); |
| 167 |
|
| 168 |
public function setFeedAtributes($feed, $formId) |
| 169 |
{ |
| 170 |
$feed['provider'] = $this->integrationKey; |
| 171 |
$feed['provider_logo'] = $this->logo; |
| 172 |
return $feed; |
| 173 |
} |
| 174 |
|
| 175 |
public function isConfigured() |
| 176 |
{ |
| 177 |
$globalStatus = $this->getApiSettings(); |
| 178 |
return $globalStatus && $globalStatus['status']; |
| 179 |
} |
| 180 |
|
| 181 |
public function isEnabled() |
| 182 |
{ |
| 183 |
$globalModules = get_option('fluentform_global_modules_status'); |
| 184 |
return $globalModules && isset($globalModules[$this->integrationKey]) && $globalModules[$this->integrationKey] == 'yes'; |
| 185 |
} |
| 186 |
|
| 187 |
public function getApiSettings() |
| 188 |
{ |
| 189 |
$settings = get_option($this->optionKey); |
| 190 |
if (!$settings || empty($settings['status'])) { |
| 191 |
$settings = [ |
| 192 |
'apiKey' => '', |
| 193 |
'status' => false |
| 194 |
]; |
| 195 |
} |
| 196 |
return $settings; |
| 197 |
} |
| 198 |
} |