| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Http\Controllers; |
| 4 |
|
| 5 |
defined('ABSPATH') or die; |
| 6 |
|
| 7 |
use FluentForm\App\Helpers\IntegrationManagerHelper; |
| 8 |
use FluentForm\Framework\Foundation\App; |
| 9 |
use FluentForm\Framework\Helpers\ArrayHelper; |
| 10 |
|
| 11 |
abstract class IntegrationManagerController extends IntegrationManagerHelper |
| 12 |
|
| 13 |
{ |
| 14 |
protected $app = null; |
| 15 |
protected $subscriber = null; |
| 16 |
protected $title = ''; |
| 17 |
protected $description = ''; |
| 18 |
protected $integrationKey = ''; |
| 19 |
protected $optionKey = ''; |
| 20 |
protected $settingsKey = ''; |
| 21 |
protected $priority = 11; |
| 22 |
public $logo = ''; |
| 23 |
public $hasGlobalMenu = true; |
| 24 |
public $category = 'crm'; |
| 25 |
public $disableGlobalSettings = 'no'; |
| 26 |
|
| 27 |
|
| 28 |
public function __construct($app, $title, $integrationKey, $optionKey, $settingsKey, $priority = 11) |
| 29 |
{ |
| 30 |
if (!$app) { |
| 31 |
$app = App::getInstance(); |
| 32 |
} |
| 33 |
$this->app = $app; |
| 34 |
$this->title = $title; |
| 35 |
$this->integrationKey = $integrationKey; |
| 36 |
$this->optionKey = $optionKey; |
| 37 |
$this->settingsKey = $settingsKey; |
| 38 |
$this->priority = $priority; |
| 39 |
|
| 40 |
// GlobalIntegrationService redacts these credentials on read; the option itself must not |
| 41 |
// be readable raw through the global-settings endpoint, which shares the prefix. |
| 42 |
add_filter('fluentform/global_settings_denied_option_keys', function ($keys) { |
| 43 |
$keys[] = $this->optionKey; |
| 44 |
return $keys; |
| 45 |
}); |
| 46 |
|
| 47 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Constructor, nonce verified in route handlers |
| 48 |
if (isset($_REQUEST['form_id'])) { |
| 49 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Constructor, nonce verified in route handlers |
| 50 |
$formId = (int)$_REQUEST['form_id']; |
| 51 |
parent::__construct( |
| 52 |
$this->settingsKey, $formId, true |
| 53 |
); |
| 54 |
} else { |
| 55 |
parent::__construct( |
| 56 |
$this->settingsKey, false, true |
| 57 |
); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
public function registerAdminHooks() |
| 62 |
{ |
| 63 |
$isEnabled = $this->isEnabled(); |
| 64 |
add_filter('fluentform/global_addons', function ($addons) use ($isEnabled) { |
| 65 |
$addons[$this->integrationKey] = [ |
| 66 |
'title' => $this->title, |
| 67 |
'category' => $this->category, |
| 68 |
'disable_global_settings' => $this->disableGlobalSettings, |
| 69 |
'description' => $this->description, |
| 70 |
'config_url' => ('yes' != $this->disableGlobalSettings) ? admin_url('admin.php?page=fluent_forms_settings#general-' . $this->integrationKey . '-settings') : '', |
| 71 |
'logo' => $this->logo, |
| 72 |
'enabled' => ($isEnabled) ? 'yes' : 'no', |
| 73 |
]; |
| 74 |
return $addons; |
| 75 |
}, $this->priority, 1); |
| 76 |
|
| 77 |
if (!$isEnabled) { |
| 78 |
return; |
| 79 |
} |
| 80 |
|
| 81 |
$this->registerNotificationHooks(); |
| 82 |
|
| 83 |
// Global Settings Here |
| 84 |
|
| 85 |
if ($this->hasGlobalMenu) { |
| 86 |
add_filter('fluentform/global_settings_components', [$this, 'addGlobalMenu']); |
| 87 |
add_filter('fluentform/global_integration_settings_' . $this->integrationKey, [$this, 'getGlobalSettings'], |
| 88 |
$this->priority, 1); |
| 89 |
add_filter('fluentform/global_integration_fields_' . $this->integrationKey, [$this, 'getGlobalFields'], |
| 90 |
$this->priority, 1); |
| 91 |
add_action('fluentform/save_global_integration_settings_' . $this->integrationKey, |
| 92 |
[$this, 'saveGlobalSettings'], $this->priority, 1); |
| 93 |
} |
| 94 |
|
| 95 |
add_filter('fluentform/global_notification_types', [$this, 'addNotificationType'], $this->priority); |
| 96 |
|
| 97 |
add_filter('fluentform/get_available_form_integrations', [$this, 'pushIntegration'], $this->priority, 2); |
| 98 |
|
| 99 |
add_filter('fluentform/global_notification_feed_' . $this->settingsKey, [$this, 'setFeedAttributes'], 10, 2); |
| 100 |
|
| 101 |
add_filter('fluentform/get_integration_defaults_' . $this->integrationKey, [$this, 'getIntegrationDefaults'], |
| 102 |
10, 2); |
| 103 |
add_filter('fluentform/get_integration_settings_fields_' . $this->integrationKey, [$this, 'getSettingsFields'], |
| 104 |
10, 2); |
| 105 |
add_filter('fluentform/get_integration_merge_fields_' . $this->integrationKey, [$this, 'getMergeFields'], 10, |
| 106 |
3); |
| 107 |
|
| 108 |
add_filter('fluentform/save_integration_settings_' . $this->integrationKey, [$this, 'setMetaKey'], 10, 2); |
| 109 |
add_filter('fluentform/get_integration_values_' . $this->integrationKey, [$this, 'prepareIntegrationFeed'], 10, |
| 110 |
3); |
| 111 |
} |
| 112 |
|
| 113 |
public function registerNotificationHooks() |
| 114 |
{ |
| 115 |
if ($this->isConfigured()) { |
| 116 |
add_filter('fluentform/global_notification_active_types', [$this, 'addActiveNotificationType'], $this->priority); |
| 117 |
add_action('fluentform/integration_notify_' . $this->settingsKey, [$this, 'notify'], $this->priority, 4); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
public function notify($feed, $formData, $entry, $form) |
| 122 |
{ |
| 123 |
// Each integration have to implement this notify method |
| 124 |
return; |
| 125 |
} |
| 126 |
|
| 127 |
public function addGlobalMenu($setting) |
| 128 |
{ |
| 129 |
$setting[$this->integrationKey] = [ |
| 130 |
'hash' => 'general-' . $this->integrationKey . '-settings', |
| 131 |
'component' => 'general-integration-settings', |
| 132 |
'settings_key' => $this->integrationKey, |
| 133 |
'title' => $this->title, |
| 134 |
]; |
| 135 |
return $setting; |
| 136 |
} |
| 137 |
|
| 138 |
public function addNotificationType($types) |
| 139 |
{ |
| 140 |
$types[] = $this->settingsKey; |
| 141 |
return $types; |
| 142 |
} |
| 143 |
|
| 144 |
public function addActiveNotificationType($types) |
| 145 |
{ |
| 146 |
$types[$this->settingsKey] = $this->integrationKey; |
| 147 |
return $types; |
| 148 |
} |
| 149 |
|
| 150 |
public function getGlobalSettings($settings) |
| 151 |
{ |
| 152 |
return $settings; |
| 153 |
} |
| 154 |
|
| 155 |
public function saveGlobalSettings($settings) |
| 156 |
{ |
| 157 |
return $settings; |
| 158 |
} |
| 159 |
|
| 160 |
public function getGlobalFields($fields) |
| 161 |
{ |
| 162 |
return $fields; |
| 163 |
} |
| 164 |
|
| 165 |
public function setMetaKey($data) |
| 166 |
{ |
| 167 |
$data['meta_key'] = $this->settingsKey; |
| 168 |
return $data; |
| 169 |
} |
| 170 |
|
| 171 |
public function prepareIntegrationFeed($setting, $feed, $formId) |
| 172 |
{ |
| 173 |
$defaults = $this->getIntegrationDefaults([], $formId); |
| 174 |
|
| 175 |
foreach ($setting as $settingKey => $settingValue) { |
| 176 |
if ('true' == $settingValue) { |
| 177 |
$setting[$settingKey] = true; |
| 178 |
} elseif ('false' == $settingValue) { |
| 179 |
$setting[$settingKey] = false; |
| 180 |
} elseif ('conditionals' == $settingKey) { |
| 181 |
if ('true' == $settingValue['status']) { |
| 182 |
$settingValue['status'] = true; |
| 183 |
} elseif ('false' == $settingValue['status']) { |
| 184 |
$settingValue['status'] = false; |
| 185 |
} |
| 186 |
$setting['conditionals'] = $settingValue; |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
if (!empty($setting['list_id'])) { |
| 191 |
$setting['list_id'] = (string)$setting['list_id']; |
| 192 |
} |
| 193 |
|
| 194 |
return wp_parse_args($setting, $defaults); |
| 195 |
} |
| 196 |
|
| 197 |
abstract public function getIntegrationDefaults($settings, $formId); |
| 198 |
|
| 199 |
abstract public function pushIntegration($integrations, $formId); |
| 200 |
|
| 201 |
abstract public function getSettingsFields($settings, $formId); |
| 202 |
|
| 203 |
abstract public function getMergeFields($list, $listId, $formId); |
| 204 |
|
| 205 |
public function setFeedAttributes($feed, $formId) |
| 206 |
{ |
| 207 |
$feed['provider'] = $this->integrationKey; |
| 208 |
$feed['provider_logo'] = $this->logo; |
| 209 |
return $feed; |
| 210 |
} |
| 211 |
|
| 212 |
public function isConfigured() |
| 213 |
{ |
| 214 |
$globalStatus = $this->getApiSettings(); |
| 215 |
return $globalStatus && $globalStatus['status']; |
| 216 |
} |
| 217 |
|
| 218 |
public function isEnabled() |
| 219 |
{ |
| 220 |
return (new \FluentForm\App\Services\Integrations\GlobalIntegrationService())->isEnabled($this->integrationKey); |
| 221 |
} |
| 222 |
|
| 223 |
public function getApiSettings() |
| 224 |
{ |
| 225 |
$settings = get_option($this->optionKey); |
| 226 |
if (!$settings || empty($settings['status'])) { |
| 227 |
$settings = [ |
| 228 |
'apiKey' => '', |
| 229 |
'status' => false, |
| 230 |
]; |
| 231 |
} |
| 232 |
return $settings; |
| 233 |
} |
| 234 |
|
| 235 |
protected function getSelectedTagIds( |
| 236 |
$data, |
| 237 |
$inputData, |
| 238 |
$simpleKey = 'tag_ids', |
| 239 |
$routingId = 'tag_ids_selection_type', |
| 240 |
$routersKey = 'tag_routers' |
| 241 |
) { |
| 242 |
$routing = ArrayHelper::get($data, $routingId, 'simple'); |
| 243 |
if (!$routing || 'simple' == $routing) { |
| 244 |
return ArrayHelper::get($data, $simpleKey, []); |
| 245 |
} |
| 246 |
|
| 247 |
$routers = ArrayHelper::get($data, $routersKey); |
| 248 |
if (empty($routers)) { |
| 249 |
return []; |
| 250 |
} |
| 251 |
|
| 252 |
return $this->evaluateRoutings($routers, $inputData); |
| 253 |
} |
| 254 |
|
| 255 |
protected function evaluateRoutings($routings, $inputData) |
| 256 |
{ |
| 257 |
$validInputs = []; |
| 258 |
foreach ($routings as $routing) { |
| 259 |
$inputValue = ArrayHelper::get($routing, 'input_value'); |
| 260 |
if (!$inputValue) { |
| 261 |
continue; |
| 262 |
} |
| 263 |
$condition = [ |
| 264 |
'conditionals' => [ |
| 265 |
'status' => true, |
| 266 |
'is_test' => true, |
| 267 |
'type' => 'any', |
| 268 |
'conditions' => [ |
| 269 |
$routing, |
| 270 |
], |
| 271 |
], |
| 272 |
]; |
| 273 |
|
| 274 |
if (\FluentForm\App\Services\ConditionAssesor::evaluate($condition, $inputData)) { |
| 275 |
$validInputs[] = $inputValue; |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
return $validInputs; |
| 280 |
} |
| 281 |
|
| 282 |
} |
| 283 |
|
| 284 |
|