| 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 WP Fluent Form pro installed', 'wpfluentform') |
| 60 |
], 423); |
| 61 |
|
| 62 |
} |
| 63 |
|
| 64 |
public function getAllFormIntegrations() |
| 65 |
{ |
| 66 |
$formId = $this->app->request->get('form_id'); |
| 67 |
|
| 68 |
$notificationKeys = apply_filters('fluentform_global_notification_types', [], $formId); |
| 69 |
|
| 70 |
$feeds = wpFluent()->table('fluentform_form_meta') |
| 71 |
->where('form_id', $formId) |
| 72 |
->whereIn('meta_key', $notificationKeys) |
| 73 |
->orderBy('id', 'DESC') |
| 74 |
->get(); |
| 75 |
|
| 76 |
$formattedFeeds = []; |
| 77 |
|
| 78 |
foreach ($feeds as $feed) { |
| 79 |
$data = json_decode($feed->value, true); |
| 80 |
$enabled = $data['enabled']; |
| 81 |
if($enabled && $enabled == 'true') { |
| 82 |
$enabled = true; |
| 83 |
} else if($enabled == 'false') { |
| 84 |
$enabled = false; |
| 85 |
} |
| 86 |
$feedData = [ |
| 87 |
'id' => $feed->id, |
| 88 |
'name' => ArrayHelper::get($data, 'name'), |
| 89 |
'enabled' => $enabled, |
| 90 |
'provider' => $feed->meta_key, |
| 91 |
'feed' => $data |
| 92 |
]; |
| 93 |
$feedData = apply_filters('fluentform_global_notification_feed_' . $feed->meta_key, $feedData, $formId); |
| 94 |
$formattedFeeds[] = $feedData; |
| 95 |
} |
| 96 |
|
| 97 |
$availableIntegrations = apply_filters('fluentform_get_available_form_integrations', [], $formId); |
| 98 |
|
| 99 |
wp_send_json_success([ |
| 100 |
'feeds' => $formattedFeeds, |
| 101 |
'available_integrations' => $availableIntegrations, |
| 102 |
'all_module_config_url' => admin_url('admin.php?page=fluent_form_add_ons') |
| 103 |
], 200); |
| 104 |
|
| 105 |
} |
| 106 |
|
| 107 |
public function updateNotificationStatus() |
| 108 |
{ |
| 109 |
$formId = $this->app->request->get('form_id'); |
| 110 |
$notificationId = $this->app->request->get('notification_id'); |
| 111 |
$status = $_REQUEST['status']; |
| 112 |
|
| 113 |
$feed = wpFluent()->table('fluentform_form_meta') |
| 114 |
->where('form_id', intval($formId)) |
| 115 |
->where('id', intval($notificationId)) |
| 116 |
->first(); |
| 117 |
|
| 118 |
$notification = json_decode($feed->value, true); |
| 119 |
|
| 120 |
|
| 121 |
if ($status == 'false' || !$status) { |
| 122 |
$notification['enabled'] = false; |
| 123 |
} else { |
| 124 |
$notification['enabled'] = true; |
| 125 |
} |
| 126 |
|
| 127 |
wpFluent()->table('fluentform_form_meta') |
| 128 |
->where('form_id', intval($formId)) |
| 129 |
->where('id', intval($notificationId)) |
| 130 |
->update([ |
| 131 |
'value' => json_encode($notification, JSON_NUMERIC_CHECK) |
| 132 |
]); |
| 133 |
|
| 134 |
$feed = wpFluent()->table('fluentform_form_meta') |
| 135 |
->where('form_id', intval($formId)) |
| 136 |
->where('id', intval($notificationId)) |
| 137 |
->first(); |
| 138 |
|
| 139 |
|
| 140 |
wp_send_json_success([ |
| 141 |
'message' => __('Integration successfully updated', 'fluentform') |
| 142 |
], 200); |
| 143 |
|
| 144 |
} |
| 145 |
|
| 146 |
public function getIntegrationSettings() |
| 147 |
{ |
| 148 |
$integrationName = $this->app->request->get('integration_name'); |
| 149 |
$integrationId = intval($this->app->request->get('integration_id')); |
| 150 |
$formId = intval($this->app->request->get('form_id')); |
| 151 |
|
| 152 |
$settings = [ |
| 153 |
'conditionals' => [ |
| 154 |
'conditions' => [], |
| 155 |
'status' => false, |
| 156 |
'type' => 'all' |
| 157 |
], |
| 158 |
'enabled' => true, |
| 159 |
'list_id' => '', |
| 160 |
'list_name' => '', |
| 161 |
'name' => '', |
| 162 |
'merge_fields' => [] |
| 163 |
]; |
| 164 |
|
| 165 |
$mergeFields = false; |
| 166 |
if ($integrationId) { |
| 167 |
$feed = wpFluent()->table('fluentform_form_meta') |
| 168 |
->where('form_id', $formId) |
| 169 |
->where('id', $integrationId) |
| 170 |
->first(); |
| 171 |
|
| 172 |
if ($feed->value) { |
| 173 |
$settings = json_decode($feed->value, true); |
| 174 |
$settings = apply_filters('fluentform_get_integration_values_' . $integrationName, $settings, $feed, $formId); |
| 175 |
if (!empty($settings['list_id'])) { |
| 176 |
$mergeFields = apply_filters('fluentform_get_integration_merge_fields_' . $integrationName, false, $settings['list_id'], $formId); |
| 177 |
} |
| 178 |
} |
| 179 |
} else { |
| 180 |
$settings = apply_filters('fluentform_get_integration_defaults_' . $integrationName, $settings, $formId); |
| 181 |
} |
| 182 |
|
| 183 |
if($settings['enabled'] == 'true') { |
| 184 |
$settings['enabled'] = true; |
| 185 |
} else if($settings['enabled'] == 'false' || $settings['enabled']) { |
| 186 |
$settings['enabled'] = false; |
| 187 |
} |
| 188 |
|
| 189 |
$settingsFields = apply_filters('fluentform_get_integration_settings_fields_' . $integrationName, [], $formId, $settings); |
| 190 |
|
| 191 |
wp_send_json_success([ |
| 192 |
'settings' => $settings, |
| 193 |
'settings_fields' => $settingsFields, |
| 194 |
'merge_fields' => $mergeFields |
| 195 |
], 200); |
| 196 |
|
| 197 |
} |
| 198 |
|
| 199 |
public function saveIntegrationSettings() |
| 200 |
{ |
| 201 |
$integrationName = $this->app->request->get('integration_name'); |
| 202 |
$integrationId = intval($this->app->request->get('integration_id')); |
| 203 |
$formId = intval($this->app->request->get('form_id')); |
| 204 |
|
| 205 |
if($this->app->request->get('data_type') == 'stringify') { |
| 206 |
$integration = \json_decode($this->app->request->get('integration'), true); |
| 207 |
} else { |
| 208 |
$integration = wp_unslash($_REQUEST['integration']); |
| 209 |
} |
| 210 |
|
| 211 |
if($integration['enabled'] && $integration['enabled'] == 'true') { |
| 212 |
$integration['status'] = true; |
| 213 |
} |
| 214 |
|
| 215 |
if(!$integration['name']) { |
| 216 |
wp_send_json_error([ |
| 217 |
'message' => 'Validation Failed', |
| 218 |
'errors' => [ |
| 219 |
'name' => ['Feed name is required'] |
| 220 |
] |
| 221 |
], 423); |
| 222 |
} |
| 223 |
|
| 224 |
$integration = apply_filters('fluentform_save_integration_value_'.$integrationName, $integration, $integrationId, $formId); |
| 225 |
|
| 226 |
|
| 227 |
$data = [ |
| 228 |
'form_id' => $formId, |
| 229 |
'meta_key' => $integrationName.'_feeds', |
| 230 |
'value' => json_encode($integration, JSON_NUMERIC_CHECK) |
| 231 |
]; |
| 232 |
|
| 233 |
$data = apply_filters('fluentform_save_integration_settings_'.$integrationName, $data, $integrationId); |
| 234 |
|
| 235 |
$created = false; |
| 236 |
if($integrationId) { |
| 237 |
wpFluent()->table('fluentform_form_meta') |
| 238 |
->where('form_id', $formId) |
| 239 |
->where('id', $integrationId) |
| 240 |
->update($data); |
| 241 |
} else { |
| 242 |
$created = true; |
| 243 |
$integrationId = wpFluent()->table('fluentform_form_meta') |
| 244 |
->insert($data); |
| 245 |
} |
| 246 |
|
| 247 |
wp_send_json_success([ |
| 248 |
'message' => __('Integration successfully saved', 'fluentform'), |
| 249 |
'integration_id' => $integrationId, |
| 250 |
'integration_name' => $integrationName, |
| 251 |
'created' => $created |
| 252 |
], 200); |
| 253 |
} |
| 254 |
|
| 255 |
public function deleteIntegrationFeed() |
| 256 |
{ |
| 257 |
$formId = intval($this->app->request->get('form_id')); |
| 258 |
$integrationId = intval($this->app->request->get('integration_id')); |
| 259 |
|
| 260 |
wpFluent()->table('fluentform_form_meta') |
| 261 |
->where('form_id', $formId) |
| 262 |
->where('id', $integrationId) |
| 263 |
->delete(); |
| 264 |
|
| 265 |
wp_send_json_success([ |
| 266 |
'message' => __('Selected integration feed successfully deleted', 'fluentform') |
| 267 |
]); |
| 268 |
} |
| 269 |
|
| 270 |
public function getIntegrationList() |
| 271 |
{ |
| 272 |
$integrationName = $this->app->request->get('integration_name'); |
| 273 |
$formId = intval($this->app->request->get('form_id')); |
| 274 |
$listId = $this->app->request->get('list_id'); |
| 275 |
$merge_fields = apply_filters('fluentform_get_integration_merge_fields_' . $integrationName, false, $listId, $formId); |
| 276 |
|
| 277 |
wp_send_json_success([ |
| 278 |
'merge_fields' => $merge_fields |
| 279 |
], 200); |
| 280 |
|
| 281 |
} |
| 282 |
} |