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