| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Services\Integrations; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use FluentForm\Framework\Support\Arr; |
| 7 |
class GlobalIntegrationService |
| 8 |
{ |
| 9 |
|
| 10 |
public function get($attr) |
| 11 |
{ |
| 12 |
$settingsKey = sanitize_text_field(Arr::get($attr, 'settings_key')); |
| 13 |
$settings = apply_filters_deprecated( |
| 14 |
'fluentform_global_integration_settings_' . $settingsKey, |
| 15 |
[ |
| 16 |
[] |
| 17 |
], |
| 18 |
FLUENTFORM_FRAMEWORK_UPGRADE, |
| 19 |
'fluentform/global_integration_settings_' . $settingsKey, |
| 20 |
'Use fluentform/global_integration_settings_' . $settingsKey . ' instead of fluentform_global_integration_settings_' . $settingsKey |
| 21 |
); |
| 22 |
$settings = apply_filters('fluentform/global_integration_settings_' . $settingsKey, $settings); |
| 23 |
$fieldSettings = apply_filters_deprecated( |
| 24 |
'fluentform_global_integration_fields_' . $settingsKey, |
| 25 |
[ |
| 26 |
[] |
| 27 |
], |
| 28 |
FLUENTFORM_FRAMEWORK_UPGRADE, |
| 29 |
'fluentform/global_integration_fields_' . $settingsKey, |
| 30 |
'Use fluentform/global_integration_fields_' . $settingsKey . ' instead of fluentform_global_integration_fields_' . $settingsKey |
| 31 |
); |
| 32 |
$fieldSettings = apply_filters('fluentform/global_integration_fields_' . $settingsKey, $fieldSettings); |
| 33 |
if (!$fieldSettings) { |
| 34 |
$message = __('Sorry! No integration failed found with: ', 'fluentform').$settingsKey; |
| 35 |
return [ |
| 36 |
'status' => false, |
| 37 |
'message'=> $message, |
| 38 |
'integration' => $settings, |
| 39 |
'settings' => $fieldSettings, |
| 40 |
]; |
| 41 |
} |
| 42 |
|
| 43 |
if (!Arr::exists($fieldSettings,'save_button_text')) { |
| 44 |
$fieldSettings['save_button_text'] = __('Save Settings', 'fluentform'); |
| 45 |
} |
| 46 |
|
| 47 |
if (!Arr::exists($fieldSettings,'valid_message')) { |
| 48 |
$fieldSettings['valid_message'] = __('Your API Key is valid', 'fluentform'); |
| 49 |
} |
| 50 |
|
| 51 |
if (!Arr::exists($fieldSettings,'invalid_message')) { |
| 52 |
$fieldSettings['invalid_message'] = __('Your API Key is not valid', 'fluentform'); |
| 53 |
} |
| 54 |
return [ |
| 55 |
'status' => true, |
| 56 |
'integration' => $settings, |
| 57 |
'settings' => $fieldSettings, |
| 58 |
]; |
| 59 |
} |
| 60 |
|
| 61 |
public function isEnabled($integrationKey) |
| 62 |
{ |
| 63 |
$globalModules = get_option('fluentform_global_modules_status'); |
| 64 |
$isEnabled = $globalModules && isset($globalModules[$integrationKey]) && 'yes' == $globalModules[$integrationKey]; |
| 65 |
|
| 66 |
return apply_filters('fluentform/is_integration_enabled_'.$integrationKey, $isEnabled); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* @param $args - key value pair array |
| 71 |
* @throws Exception |
| 72 |
* @return void |
| 73 |
*/ |
| 74 |
public function updateModuleStatus($args) { |
| 75 |
$moduleKey = sanitize_text_field(Arr::get($args, 'module_key')); |
| 76 |
$moduleStatus = sanitize_text_field(Arr::get($args, 'module_status')); |
| 77 |
if (!$moduleKey || !in_array($moduleStatus, ['yes', 'no'])) { |
| 78 |
// phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- Exception message, not output |
| 79 |
throw new Exception(__('Status update failed. Not valid module or status', 'fluentform')); |
| 80 |
} |
| 81 |
try { |
| 82 |
$modules = (array)get_option('fluentform_global_modules_status'); |
| 83 |
$modules[$moduleKey] = $moduleStatus; |
| 84 |
update_option('fluentform_global_modules_status', $modules, 'no'); |
| 85 |
} catch (Exception $e) { |
| 86 |
// phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- Exception message, not output |
| 87 |
throw new Exception($e->getMessage()); |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
|