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