| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Services\Integrations; |
| 4 |
|
| 5 |
use FluentForm\Framework\Foundation\Application; |
| 6 |
use FluentForm\Framework\Helpers\ArrayHelper; |
| 7 |
|
| 8 |
class GlobalIntegrationManager |
| 9 |
{ |
| 10 |
|
| 11 |
private $app; |
| 12 |
|
| 13 |
public function __construct(Application $app) |
| 14 |
{ |
| 15 |
$this->app = $app; |
| 16 |
} |
| 17 |
|
| 18 |
public function getGlobalSettingsAjax() |
| 19 |
{ |
| 20 |
$settingsKey = sanitize_text_field($_REQUEST['settings_key']); |
| 21 |
$settings = apply_filters('fluentform_global_integration_settings_' . $settingsKey, []); |
| 22 |
$fieldSettings = apply_filters('fluentform_global_integration_fields_' . $settingsKey, []); |
| 23 |
|
| 24 |
if (!$fieldSettings) { |
| 25 |
wp_send_json_error([ |
| 26 |
'settings' => $settings, |
| 27 |
'settings_key' => $settingsKey, |
| 28 |
'message' => __('Sorry! No integration failed found with: ', 'fluentform') . $settingsKey |
| 29 |
], 423); |
| 30 |
} |
| 31 |
|
| 32 |
if (empty($fieldSettings['save_button_text'])) { |
| 33 |
$fieldSettings['save_button_text'] = __('Save Settings', 'fluentform'); |
| 34 |
} |
| 35 |
|
| 36 |
if (empty($fieldSettings['valid_message'])) { |
| 37 |
$fieldSettings['valid_message'] = __('Your API Key is valid', 'fluentform'); |
| 38 |
} |
| 39 |
|
| 40 |
if (empty($fieldSettings['invalid_message'])) { |
| 41 |
$fieldSettings['invalid_message'] = __('Your API Key is not valid', 'fluentform'); |
| 42 |
} |
| 43 |
|
| 44 |
wp_send_json_success([ |
| 45 |
'integration' => $settings, |
| 46 |
'settings' => $fieldSettings |
| 47 |
], 200); |
| 48 |
|
| 49 |
} |
| 50 |
|
| 51 |
public function saveGlobalSettingsAjax() |
| 52 |
{ |
| 53 |
$settingsKey = sanitize_text_field($_REQUEST['settings_key']); |
| 54 |
$integration = wp_unslash($_REQUEST['integration']); |
| 55 |
do_action('fluentform_save_global_integration_settings_' . $settingsKey, $integration); |
| 56 |
|
| 57 |
// Someone should catch that above action and send response |
| 58 |
wp_send_json_error([ |
| 59 |
'message' => __('Sorry, no Integration found. Please make sure that latest version of Fluent Forms pro installed', 'fluentform') |
| 60 |
], 423); |
| 61 |
|
| 62 |
} |
| 63 |
|
| 64 |
public function getAllFormIntegrations() |
| 65 |
{ |
| 66 |
$formId = $this->app->request->get('form_id'); |
| 67 |
|
| 68 |
$formattedFeeds = $this->getNotificationFeeds($formId); |
| 69 |
|
| 70 |
$availableIntegrations = apply_filters('fluentform_get_available_form_integrations', [], $formId); |
| 71 |
|
| 72 |
wp_send_json_success([ |
| 73 |
'feeds' => $formattedFeeds, |
| 74 |
'available_integrations' => $availableIntegrations, |
| 75 |
'all_module_config_url' => admin_url('admin.php?page=fluent_forms_add_ons') |
| 76 |
], 200); |
| 77 |
|
| 78 |
} |
| 79 |
|
| 80 |
public function getNotificationFeeds($formId) |
| 81 |
{ |
| 82 |
$notificationKeys = apply_filters('fluentform_global_notification_types', [], $formId); |
| 83 |
|
| 84 |
if ($notificationKeys) { |
| 85 |
$feeds = wpFluent()->table('fluentform_form_meta') |
| 86 |
->where('form_id', $formId) |
| 87 |
->whereIn('meta_key', $notificationKeys) |
| 88 |
->orderBy('id', 'DESC') |
| 89 |
->get(); |
| 90 |
} else { |
| 91 |
$feeds = []; |
| 92 |
} |
| 93 |
|
| 94 |
$formattedFeeds = []; |
| 95 |
|
| 96 |
foreach ($feeds as $feed) { |
| 97 |
$data = json_decode($feed->value, true); |
| 98 |
$enabled = $data['enabled']; |
| 99 |
if ($enabled && $enabled == 'true') { |
| 100 |
$enabled = true; |
| 101 |
} else if ($enabled == 'false') { |
| 102 |
$enabled = false; |
| 103 |
} |
| 104 |
$feedData = [ |
| 105 |
'id' => $feed->id, |
| 106 |
'name' => ArrayHelper::get($data, 'name'), |
| 107 |
'enabled' => $enabled, |
| 108 |
'provider' => $feed->meta_key, |
| 109 |
'feed' => $data |
| 110 |
]; |
| 111 |
$feedData = apply_filters('fluentform_global_notification_feed_' . $feed->meta_key, $feedData, $formId); |
| 112 |
$formattedFeeds[] = $feedData; |
| 113 |
} |
| 114 |
return $formattedFeeds; |
| 115 |
} |
| 116 |
|
| 117 |
public function updateNotificationStatus() |
| 118 |
{ |
| 119 |
$formId = intval($this->app->request->get('form_id')); |
| 120 |
$notificationId = intval($this->app->request->get('notification_id')); |
| 121 |
$status = sanitize_key($this->app->request->get('status')); |
| 122 |
|
| 123 |
$feed = wpFluent()->table('fluentform_form_meta') |
| 124 |
->where('form_id', $formId) |
| 125 |
->where('id', $notificationId) |
| 126 |
->first(); |
| 127 |
|
| 128 |
$notification = json_decode($feed->value, true); |
| 129 |
|
| 130 |
|
| 131 |
if ($status == 'false' || !$status) { |
| 132 |
$notification['enabled'] = false; |
| 133 |
} else { |
| 134 |
$notification['enabled'] = true; |
| 135 |
} |
| 136 |
|
| 137 |
wpFluent()->table('fluentform_form_meta') |
| 138 |
->where('form_id', $formId) |
| 139 |
->where('id', $notificationId) |
| 140 |
->update([ |
| 141 |
'value' => json_encode($notification, JSON_NUMERIC_CHECK) |
| 142 |
]); |
| 143 |
|
| 144 |
$feed = wpFluent()->table('fluentform_form_meta') |
| 145 |
->where('form_id', $formId) |
| 146 |
->where('id', $notificationId) |
| 147 |
->first(); |
| 148 |
|
| 149 |
|
| 150 |
wp_send_json_success([ |
| 151 |
'message' => __('Integration successfully updated', 'fluentform') |
| 152 |
], 200); |
| 153 |
|
| 154 |
} |
| 155 |
|
| 156 |
public function getIntegrationSettings() |
| 157 |
{ |
| 158 |
$integrationName = $this->app->request->get('integration_name'); |
| 159 |
$integrationId = intval($this->app->request->get('integration_id')); |
| 160 |
$formId = intval($this->app->request->get('form_id')); |
| 161 |
|
| 162 |
$settings = [ |
| 163 |
'conditionals' => [ |
| 164 |
'conditions' => [], |
| 165 |
'status' => false, |
| 166 |
'type' => 'all' |
| 167 |
], |
| 168 |
'enabled' => true, |
| 169 |
'list_id' => '', |
| 170 |
'list_name' => '', |
| 171 |
'name' => '', |
| 172 |
'merge_fields' => [] |
| 173 |
]; |
| 174 |
|
| 175 |
$mergeFields = false; |
| 176 |
if ($integrationId) { |
| 177 |
$feed = wpFluent()->table('fluentform_form_meta') |
| 178 |
->where('form_id', $formId) |
| 179 |
->where('id', $integrationId) |
| 180 |
->first(); |
| 181 |
|
| 182 |
if ($feed->value) { |
| 183 |
$settings = json_decode($feed->value, true); |
| 184 |
|
| 185 |
$settings = apply_filters('fluentform_get_integration_values_' . $integrationName, $settings, $feed, $formId); |
| 186 |
if (!empty($settings['list_id'])) { |
| 187 |
$mergeFields = apply_filters('fluentform_get_integration_merge_fields_' . $integrationName, false, $settings['list_id'], $formId); |
| 188 |
} |
| 189 |
} |
| 190 |
} else { |
| 191 |
$settings = apply_filters('fluentform_get_integration_defaults_' . $integrationName, $settings, $formId); |
| 192 |
} |
| 193 |
|
| 194 |
if ($settings['enabled'] == 'true') { |
| 195 |
$settings['enabled'] = true; |
| 196 |
} else if ($settings['enabled'] == 'false' || $settings['enabled']) { |
| 197 |
$settings['enabled'] = false; |
| 198 |
} |
| 199 |
|
| 200 |
$settingsFields = apply_filters('fluentform_get_integration_settings_fields_' . $integrationName, $settings, $formId, $settings); |
| 201 |
|
| 202 |
|
| 203 |
wp_send_json_success([ |
| 204 |
'settings' => $settings, |
| 205 |
'settings_fields' => $settingsFields, |
| 206 |
'merge_fields' => $mergeFields |
| 207 |
], 200); |
| 208 |
|
| 209 |
} |
| 210 |
|
| 211 |
public function saveIntegrationSettings() |
| 212 |
{ |
| 213 |
$integrationName = $this->app->request->get('integration_name'); |
| 214 |
$integrationId = intval($this->app->request->get('integration_id')); |
| 215 |
$formId = intval($this->app->request->get('form_id')); |
| 216 |
|
| 217 |
if ($this->app->request->get('data_type') == 'stringify') { |
| 218 |
$integration = \json_decode($this->app->request->get('integration'), true); |
| 219 |
} else { |
| 220 |
$integration = wp_unslash($_REQUEST['integration']); |
| 221 |
} |
| 222 |
|
| 223 |
if ($integration['enabled'] && $integration['enabled'] == 'true') { |
| 224 |
$integration['status'] = true; |
| 225 |
} |
| 226 |
|
| 227 |
if (!$integration['name']) { |
| 228 |
wp_send_json_error([ |
| 229 |
'message' => 'Validation Failed', |
| 230 |
'errors' => [ |
| 231 |
'name' => ['Feed name is required'] |
| 232 |
] |
| 233 |
], 423); |
| 234 |
} |
| 235 |
|
| 236 |
$integration = apply_filters('fluentform_save_integration_value_' . $integrationName, $integration, $integrationId, $formId); |
| 237 |
|
| 238 |
$data = [ |
| 239 |
'form_id' => $formId, |
| 240 |
'meta_key' => $integrationName . '_feeds', |
| 241 |
'value' => \json_encode($integration) |
| 242 |
]; |
| 243 |
|
| 244 |
$data = apply_filters('fluentform_save_integration_settings_' . $integrationName, $data, $integrationId); |
| 245 |
|
| 246 |
$created = false; |
| 247 |
if ($integrationId) { |
| 248 |
wpFluent()->table('fluentform_form_meta') |
| 249 |
->where('form_id', $formId) |
| 250 |
->where('id', $integrationId) |
| 251 |
->update($data); |
| 252 |
} else { |
| 253 |
$created = true; |
| 254 |
$integrationId = wpFluent()->table('fluentform_form_meta') |
| 255 |
->insert($data); |
| 256 |
} |
| 257 |
|
| 258 |
wp_send_json_success([ |
| 259 |
'message' => __('Integration successfully saved', 'fluentform'), |
| 260 |
'integration_id' => $integrationId, |
| 261 |
'integration_name' => $integrationName, |
| 262 |
'created' => $created |
| 263 |
], 200); |
| 264 |
} |
| 265 |
|
| 266 |
public function deleteIntegrationFeed() |
| 267 |
{ |
| 268 |
$formId = intval($this->app->request->get('form_id')); |
| 269 |
$integrationId = intval($this->app->request->get('integration_id')); |
| 270 |
|
| 271 |
wpFluent()->table('fluentform_form_meta') |
| 272 |
->where('form_id', $formId) |
| 273 |
->where('id', $integrationId) |
| 274 |
->delete(); |
| 275 |
|
| 276 |
wp_send_json_success([ |
| 277 |
'message' => __('Selected integration feed successfully deleted', 'fluentform') |
| 278 |
]); |
| 279 |
} |
| 280 |
|
| 281 |
public function getIntegrationList() |
| 282 |
{ |
| 283 |
$integrationName = $this->app->request->get('integration_name'); |
| 284 |
$formId = intval($this->app->request->get('form_id')); |
| 285 |
$listId = $this->app->request->get('list_id'); |
| 286 |
$merge_fields = apply_filters('fluentform_get_integration_merge_fields_' . $integrationName, false, $listId, $formId); |
| 287 |
|
| 288 |
wp_send_json_success([ |
| 289 |
'merge_fields' => $merge_fields |
| 290 |
], 200); |
| 291 |
|
| 292 |
} |
| 293 |
} |
| 294 |
|