| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Helpers; |
| 4 |
|
| 5 |
|
| 6 |
use FluentForm\App\Models\FormMeta; |
| 7 |
use FluentForm\App\Services\Integrations\FormIntegrationService; |
| 8 |
|
| 9 |
class IntegrationManagerHelper |
| 10 |
{ |
| 11 |
protected $settingsKey; |
| 12 |
protected $formId; |
| 13 |
protected $isMultiple; |
| 14 |
|
| 15 |
protected $integrationService; |
| 16 |
|
| 17 |
public function __construct($settingsKey = '', $form_id = false, $isMultiple = false) |
| 18 |
{ |
| 19 |
$this->settingsKey = $settingsKey; |
| 20 |
$this->isMultiple = $isMultiple; |
| 21 |
$this->formId = $form_id; |
| 22 |
$this->integrationService = new FormIntegrationService(); |
| 23 |
} |
| 24 |
|
| 25 |
public function get($settingsId) |
| 26 |
{ |
| 27 |
$settings = FormMeta::where('form_id', $this->formId) |
| 28 |
->where('meta_key', $this->settingsKey) |
| 29 |
->find($settingsId); |
| 30 |
$settings->formattedValue = $this->getFormattedValue($settings); |
| 31 |
return $settings; |
| 32 |
} |
| 33 |
|
| 34 |
public function save($settings) |
| 35 |
{ |
| 36 |
return FormMeta::insertGetId([ |
| 37 |
'meta_key' => $this->settingsKey, |
| 38 |
'form_id' => $this->formId, |
| 39 |
'value' => json_encode($settings), |
| 40 |
]); |
| 41 |
} |
| 42 |
|
| 43 |
public function update($settingsId, $settings) |
| 44 |
{ |
| 45 |
return FormMeta::where('id', $settingsId) |
| 46 |
->update([ |
| 47 |
'value' => json_encode($settings), |
| 48 |
]); |
| 49 |
} |
| 50 |
|
| 51 |
public function delete($settingsId) |
| 52 |
{ |
| 53 |
FormMeta::where('id', $settingsId) |
| 54 |
->delete(); |
| 55 |
} |
| 56 |
|
| 57 |
public function getAll() |
| 58 |
{ |
| 59 |
$settingsQuery = FormMeta::where('form_id', $this->formId) |
| 60 |
->where('meta_key', $this->settingsKey); |
| 61 |
|
| 62 |
if ($this->isMultiple) { |
| 63 |
$settings = $settingsQuery->get(); |
| 64 |
foreach ($settings as $setting) { |
| 65 |
$setting->formattedValue = $this->getFormattedValue($setting); |
| 66 |
} |
| 67 |
} else { |
| 68 |
$settings = $settingsQuery->first(); |
| 69 |
$settings->formattedValue = $this->getFormattedValue($settings); |
| 70 |
} |
| 71 |
return $settings; |
| 72 |
} |
| 73 |
|
| 74 |
protected function logResponse($response, $feed, $data, $form, $entryId, $status) |
| 75 |
{ |
| 76 |
if (!$response) { |
| 77 |
return; |
| 78 |
} |
| 79 |
|
| 80 |
$oldAction = 'fluentform_after_submission_api_response_' . $status; |
| 81 |
$newAction = 'fluentform/after_submission_api_response_' . $status; |
| 82 |
|
| 83 |
do_action_deprecated( |
| 84 |
$oldAction, |
| 85 |
[ |
| 86 |
$form, |
| 87 |
$entryId, |
| 88 |
$data, |
| 89 |
$feed, |
| 90 |
$response, |
| 91 |
$this->getApiResponseMessage($response, $status) |
| 92 |
], |
| 93 |
FLUENTFORM_FRAMEWORK_UPGRADE, |
| 94 |
$newAction, |
| 95 |
'Use ' . $newAction . ' instead of ' .$oldAction |
| 96 |
); |
| 97 |
|
| 98 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.DynamicHooknameFound -- Dynamic hook name constructed from deprecated hook |
| 99 |
do_action( |
| 100 |
$newAction, // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.DynamicHooknameFound -- Dynamic hook name constructed from deprecated hook |
| 101 |
$form, |
| 102 |
$entryId, |
| 103 |
$data, |
| 104 |
$feed, |
| 105 |
$response, |
| 106 |
$this->getApiResponseMessage($response, $status) |
| 107 |
); |
| 108 |
} |
| 109 |
|
| 110 |
protected function getApiResponseMessage($response, $status) |
| 111 |
{ |
| 112 |
if (is_array($response) && isset($response['message'])) { |
| 113 |
return $response['message']; |
| 114 |
} |
| 115 |
|
| 116 |
return $status; |
| 117 |
} |
| 118 |
|
| 119 |
public function getFormattedValue($setting) |
| 120 |
{ |
| 121 |
if (Helper::isJson($setting->value)) { |
| 122 |
return json_decode($setting->value, true); |
| 123 |
} |
| 124 |
|
| 125 |
return $setting->value; |
| 126 |
} |
| 127 |
|
| 128 |
public static function isIntegrationEnabled($key) |
| 129 |
{ |
| 130 |
static $globalModules = []; |
| 131 |
|
| 132 |
if (empty($globalModules)) { |
| 133 |
$globalModules = get_option('fluentform_global_modules_status'); |
| 134 |
} |
| 135 |
if (\FluentForm\Framework\Helpers\ArrayHelper::get($globalModules, $key) == 'yes') { |
| 136 |
return true; |
| 137 |
} |
| 138 |
|
| 139 |
return false; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
} |
| 144 |
|