| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Modules\Integrations; |
| 4 |
|
| 5 |
use FluentCart\App\Helpers\EditorShortCodeHelper; |
| 6 |
use FluentCart\App\Models\Meta; |
| 7 |
use FluentCart\App\Models\ProductMeta; |
| 8 |
use FluentCart\Framework\Http\Request\Request; |
| 9 |
use FluentCart\Framework\Support\Arr; |
| 10 |
use FluentCart\App\Services\PluginInstaller\BackgroundInstaller; |
| 11 |
|
| 12 |
// Load necessary WordPress files for filesystem and plugin installation |
| 13 |
require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
| 14 |
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 15 |
require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 16 |
require_once ABSPATH . 'wp-admin/includes/misc.php'; |
| 17 |
require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php'; |
| 18 |
|
| 19 |
class GlobalIntegrationSettings |
| 20 |
{ |
| 21 |
public function getGlobalSettingsData($request) |
| 22 |
{ |
| 23 |
$settingsKey = sanitize_text_field(Arr::get($request, 'settings_key')); |
| 24 |
$settings = apply_filters('fluent_cart/integration/global_integration_settings_' . $settingsKey, [], []); |
| 25 |
$fieldSettings = apply_filters('fluent_cart/integration/global_integration_fields_' . $settingsKey, [], []); |
| 26 |
|
| 27 |
if (!$fieldSettings) { |
| 28 |
wp_send_json([ |
| 29 |
'settings' => $settings, |
| 30 |
'settings_key' => $settingsKey, |
| 31 |
'message' => __('Sorry! No integration failed found with: ', 'fluent-cart') . $settingsKey |
| 32 |
], 422); |
| 33 |
} |
| 34 |
|
| 35 |
if (empty($fieldSettings['save_button_text'])) { |
| 36 |
$fieldSettings['save_button_text'] = __('Save Settings', 'fluent-cart'); |
| 37 |
} |
| 38 |
|
| 39 |
if (empty($fieldSettings['valid_message'])) { |
| 40 |
$fieldSettings['valid_message'] = __('Your API Key is valid', 'fluent-cart'); |
| 41 |
} |
| 42 |
|
| 43 |
if (empty($fieldSettings['invalid_message'])) { |
| 44 |
$fieldSettings['invalid_message'] = __('Your API Key is not valid', 'fluent-cart'); |
| 45 |
} |
| 46 |
|
| 47 |
wp_send_json([ |
| 48 |
'integration' => $settings, |
| 49 |
'settings' => $fieldSettings |
| 50 |
], 200); |
| 51 |
} |
| 52 |
|
| 53 |
public function authenticateCredentials($request) |
| 54 |
{ |
| 55 |
$settingsKey = sanitize_text_field(Arr::get($request, 'settings_key')); |
| 56 |
$integration = wp_unslash(Arr::get($request, 'integration')); |
| 57 |
do_action('fluent_cart/integration/authenticate_global_credentials_' . $settingsKey, [ |
| 58 |
'settings_key' => $settingsKey, |
| 59 |
'integration' => $integration |
| 60 |
]); |
| 61 |
} |
| 62 |
|
| 63 |
public function saveGlobalSettingsData($request) |
| 64 |
{ |
| 65 |
$settingsKey = sanitize_text_field(Arr::get($request, 'settings_key')); |
| 66 |
$integration = wp_unslash(Arr::get($request, 'integration')); |
| 67 |
do_action('fluent_cart/integration/save_global_integration_settings_' . $settingsKey, [ |
| 68 |
'settings_key' => $settingsKey, |
| 69 |
'integration' => $integration |
| 70 |
]); |
| 71 |
|
| 72 |
// Someone should catch that above action and send response |
| 73 |
wp_send_json([ |
| 74 |
'message' => __('Sorry, no Integration found. Please make sure that latest version of installed', 'fluent-cart') |
| 75 |
], 422); |
| 76 |
} |
| 77 |
|
| 78 |
// Get All the feeds for global integrations |
| 79 |
// This is used in the global integration settings page |
| 80 |
// It returns all the feeds that are registered with the global scope |
| 81 |
public function getFeeds() |
| 82 |
{ |
| 83 |
|
| 84 |
$availableIntegrations = apply_filters('fluent_cart/integration/order_integrations', [], []); |
| 85 |
|
| 86 |
$availableIntegrations = array_filter($availableIntegrations, function ($integration) { |
| 87 |
return in_array('global', Arr::get($integration, 'scopes', [])) && $integration['enabled']; |
| 88 |
}); |
| 89 |
|
| 90 |
$formattedFeeds = []; |
| 91 |
if ($availableIntegrations) { |
| 92 |
$feeds = Meta::whereIn('meta_key', array_keys($availableIntegrations)) |
| 93 |
->where('object_type', 'order_integration') |
| 94 |
->orderBy('id', 'ASC') |
| 95 |
->get(); |
| 96 |
|
| 97 |
foreach ($feeds as $feed) { |
| 98 |
$data = $feed->meta_value; |
| 99 |
$formattedFeeds[] = [ |
| 100 |
'id' => $feed->id, |
| 101 |
'name' => Arr::get($data, 'name'), |
| 102 |
'enabled' => $data['enabled'], |
| 103 |
'provider' => $feed->meta_key, |
| 104 |
'feed' => $data, |
| 105 |
'scope' => 'product', |
| 106 |
]; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
return [ |
| 111 |
'feeds' => $formattedFeeds, |
| 112 |
'available_integrations' => $availableIntegrations, |
| 113 |
'all_module_config_url' => admin_url('admin.php?page=fluent-cart#/integrations') |
| 114 |
]; |
| 115 |
} |
| 116 |
|
| 117 |
public function getNotificationFeeds() |
| 118 |
{ |
| 119 |
$notificationKeys = apply_filters('fluent_cart/integration/global_notification_types', [], []); |
| 120 |
if ($notificationKeys) { |
| 121 |
$feeds = Meta::whereIn('meta_key', $notificationKeys) |
| 122 |
->orderBy('id', 'DESC') |
| 123 |
->get(); |
| 124 |
} else { |
| 125 |
$feeds = []; |
| 126 |
} |
| 127 |
return $this->formatFeedsData($feeds); |
| 128 |
} |
| 129 |
|
| 130 |
public function formatFeedsData($feeds): array |
| 131 |
{ |
| 132 |
$formattedFeeds = []; |
| 133 |
foreach ($feeds as $feed) { |
| 134 |
$formattedFeeds[] = $this->formatFeedData($feed); |
| 135 |
} |
| 136 |
return $formattedFeeds; |
| 137 |
} |
| 138 |
|
| 139 |
public function formatFeedData($feed) |
| 140 |
{ |
| 141 |
$data = !is_string($feed->meta_value) ? $feed->meta_value : json_decode($feed->meta_value, true); |
| 142 |
|
| 143 |
$feedData = [ |
| 144 |
'id' => $feed['id'], |
| 145 |
'name' => Arr::get($data, 'name'), |
| 146 |
'enabled' => $data['enabled'], |
| 147 |
'provider' => $feed->meta_key, |
| 148 |
'feed' => $data |
| 149 |
]; |
| 150 |
|
| 151 |
return apply_filters('fluent_cart/integration/global_notification_feed_' . $feed->meta_key, $feedData, []); |
| 152 |
} |
| 153 |
|
| 154 |
// public function updateNotificationStatus($request) |
| 155 |
// { |
| 156 |
// $integrations = Arr::get($request, 'addons'); |
| 157 |
|
| 158 |
// fct_update_option('fluent_cart_global_integrations', $integrations); |
| 159 |
|
| 160 |
// return [ |
| 161 |
// 'message' => __('Integration successfully updated', 'fluent-cart') |
| 162 |
// ]; |
| 163 |
// } |
| 164 |
|
| 165 |
public function getIntegrationSettings($args, $feedType = 'global') |
| 166 |
{ |
| 167 |
$integrationName = Arr::get($args, 'integration_name'); |
| 168 |
$integrationId = intval(Arr::get($args, 'integration_id')); |
| 169 |
|
| 170 |
$settings = [ |
| 171 |
'conditionals' => [ |
| 172 |
'conditions' => [], |
| 173 |
'status' => false, |
| 174 |
'type' => 'all' |
| 175 |
], |
| 176 |
'enabled' => 'yes', |
| 177 |
'list_id' => '', |
| 178 |
'list_name' => '', |
| 179 |
'name' => '', |
| 180 |
'merge_fields' => (object)[], |
| 181 |
]; |
| 182 |
|
| 183 |
$mergeFields = false; |
| 184 |
if ($integrationId) { |
| 185 |
if ($feedType === 'product') { |
| 186 |
$feed = ProductMeta::query()->where('id', $integrationId)->first(); |
| 187 |
} else { |
| 188 |
$feed = Meta::query()->where('id', $integrationId)->first(); |
| 189 |
} |
| 190 |
|
| 191 |
if (!$feed) { |
| 192 |
return new \WP_Error( |
| 193 |
'integration_not_found', |
| 194 |
__('Integration not found', 'fluent-cart') |
| 195 |
); |
| 196 |
} |
| 197 |
|
| 198 |
$feedValue = $feed->meta_value ?? []; |
| 199 |
$settings = wp_parse_args($feedValue, apply_filters('fluent_cart/integration/get_integration_defaults_' . $integrationName, [], [])); |
| 200 |
} else { |
| 201 |
$settings = apply_filters('fluent_cart/integration/get_integration_defaults_' . $integrationName, $settings, []); |
| 202 |
} |
| 203 |
|
| 204 |
$settingsFields = apply_filters('fluent_cart/integration/get_integration_settings_fields_' . $integrationName, [], $settings); |
| 205 |
$shortCodes = EditorShortCodeHelper::getShortCodes(); |
| 206 |
|
| 207 |
return [ |
| 208 |
'settings' => $settings, |
| 209 |
'settings_fields' => $settingsFields, |
| 210 |
'shortcodes' => $shortCodes, |
| 211 |
'inputs' => EditorShortCodeHelper::checkoutInputs(), |
| 212 |
'merge_fields' => $settings['merge_fields'] ?? $mergeFields |
| 213 |
]; |
| 214 |
|
| 215 |
} |
| 216 |
|
| 217 |
public function prepareFeedData($data, $integrationId, $integrationName, $feedType = 'order_integration') |
| 218 |
{ |
| 219 |
|
| 220 |
if (!$integrationName) { |
| 221 |
wp_send_json([ |
| 222 |
'message' => __('Validation Failed', 'fluent-cart'), |
| 223 |
'errors' => [ |
| 224 |
'name' => [ |
| 225 |
__('Feed name is required', 'fluent-cart') |
| 226 |
] |
| 227 |
] |
| 228 |
], 422); |
| 229 |
} |
| 230 |
|
| 231 |
$errors = $this->validate($data, $integrationName); |
| 232 |
|
| 233 |
if (!empty($errors)) { |
| 234 |
|
| 235 |
return new \WP_Error( |
| 236 |
'validation_error', |
| 237 |
__('Validation Failed, Please fill up the required fields.', 'fluent-cart'), |
| 238 |
$errors |
| 239 |
); |
| 240 |
} |
| 241 |
|
| 242 |
if (Arr::get($data, 'data_type') == 'stringify') { |
| 243 |
$integration = \json_decode(Arr::get($data, 'integration'), true); |
| 244 |
} else { |
| 245 |
$integration = wp_unslash(Arr::get($data, 'integration')); |
| 246 |
} |
| 247 |
|
| 248 |
$integration = apply_filters('fluent_cart/integration/save_integration_values_' . $integrationName, $integration, [ |
| 249 |
'id' => $integrationId, |
| 250 |
'provider' => $integrationName, |
| 251 |
'feed_type' => $feedType, |
| 252 |
'data' => $data |
| 253 |
]); |
| 254 |
|
| 255 |
return [ |
| 256 |
//phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key |
| 257 |
'meta_key' => sanitize_text_field($integrationName), |
| 258 |
//phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value |
| 259 |
'meta_value' => $integration, |
| 260 |
'object_type' => $feedType |
| 261 |
]; |
| 262 |
} |
| 263 |
|
| 264 |
public function validate($data, $integrationName) |
| 265 |
{ |
| 266 |
if (empty($data['integration'])) { |
| 267 |
return [ |
| 268 |
'message' => __('Validation Failed, No valid data found.', 'fluent-cart'), |
| 269 |
'errors' => [ |
| 270 |
'name' => [__('Feed data is required', 'fluent-cart')] |
| 271 |
] |
| 272 |
]; |
| 273 |
} |
| 274 |
|
| 275 |
$data = json_decode($data['integration'], true); |
| 276 |
$settingsFields = apply_filters('fluent_cart/integration/get_integration_settings_fields_' . $integrationName, [], []); |
| 277 |
$fields = Arr::get($settingsFields, 'fields'); |
| 278 |
return $this->validateFields($fields, $data); |
| 279 |
} |
| 280 |
|
| 281 |
public function validateFields(array $fields, array $data): array |
| 282 |
{ |
| 283 |
$errors = []; |
| 284 |
foreach ($fields as $field) { |
| 285 |
$key = Arr::get($field, 'key'); |
| 286 |
if (empty($field['required'])) { |
| 287 |
continue; |
| 288 |
} |
| 289 |
if (!array_key_exists($key, $data) || (is_array($data[$key]) ? empty($data[$key]) : trim((string)$data[$key]) === '')) { |
| 290 |
$label = $field['label'] ?? $key; |
| 291 |
$errors[$key] = "$label is required."; |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
return $errors; |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
public function saveIntegrationSettings($reuestData) |
| 300 |
{ |
| 301 |
$integrationId = Arr::get($reuestData, 'integration_id'); |
| 302 |
$integrationMeta = null; |
| 303 |
if ($integrationId) { |
| 304 |
$integrationMeta = Meta::query()->where('id', $integrationId) |
| 305 |
->where('object_type', 'order_integration') |
| 306 |
->first(); |
| 307 |
|
| 308 |
if (!$integrationMeta) { |
| 309 |
return new \WP_Error( |
| 310 |
'integration_not_found', |
| 311 |
__('Integration not found', 'fluent-cart') |
| 312 |
); |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
$provider = Arr::get($reuestData, 'integration_name'); |
| 317 |
|
| 318 |
$integrationFeed = json_decode(Arr::get($reuestData, 'integration'), true); |
| 319 |
|
| 320 |
$integrationFeedData = IntegrationHelper::validateAndFormatIntegrationFeedSettings($integrationFeed, [ |
| 321 |
'provider' => $provider, |
| 322 |
'scope' => 'global', |
| 323 |
'integration_id' => $integrationId |
| 324 |
]); |
| 325 |
|
| 326 |
|
| 327 |
if (is_wp_error($integrationFeedData)) { |
| 328 |
return $integrationFeedData; |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
if ($integrationMeta) { |
| 333 |
$integrationMeta->meta_value = $integrationFeedData; |
| 334 |
$integrationMeta->save(); |
| 335 |
} else { |
| 336 |
$integrationMeta = Meta::create([ |
| 337 |
//phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key |
| 338 |
'meta_key' => $provider, |
| 339 |
//phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value |
| 340 |
'meta_value' => $integrationFeedData, |
| 341 |
'object_type' => 'order_integration' |
| 342 |
]); |
| 343 |
} |
| 344 |
|
| 345 |
return [ |
| 346 |
'message' => __('Integration successfully saved', 'fluent-cart'), |
| 347 |
'integration_id' => $integrationId, |
| 348 |
'integration_name' => $provider, |
| 349 |
'created' => $integrationMeta->wasRecentlyCreated |
| 350 |
]; |
| 351 |
} |
| 352 |
|
| 353 |
public function deleteIntegrationFeed($request) |
| 354 |
{ |
| 355 |
$integrationId = intval(Arr::get($request, 'integration_id')); |
| 356 |
|
| 357 |
Meta::where('id', $integrationId)->delete(); |
| 358 |
|
| 359 |
return [ |
| 360 |
'message' => __('Selected integration feed successfully deleted', 'fluent-cart') |
| 361 |
]; |
| 362 |
} |
| 363 |
|
| 364 |
public function getIntegrationList($request) |
| 365 |
{ |
| 366 |
$integrationName = Arr::get($request, 'integration_name'); |
| 367 |
$list = []; |
| 368 |
$listId = Arr::get($request, 'list_id'); |
| 369 |
$merge_fields = apply_filters('fluent_cart/integration/get_integration_merge_fields_' . $integrationName, $list, $listId); |
| 370 |
return [ |
| 371 |
'merge_fields' => $merge_fields |
| 372 |
]; |
| 373 |
} |
| 374 |
|
| 375 |
public function chainedData($requestData) |
| 376 |
{ |
| 377 |
do_action('fluent_cart/integration/chained_' . Arr::get($requestData, 'route'), [ |
| 378 |
'data' => $requestData |
| 379 |
]); |
| 380 |
} |
| 381 |
|
| 382 |
public function installPlugin($pluginSlug) |
| 383 |
{ |
| 384 |
if (!function_exists('is_plugin_active')) { |
| 385 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 386 |
} |
| 387 |
|
| 388 |
if (!current_user_can('install_plugins')) { |
| 389 |
return new \WP_Error('permission_denied', __('You do not have permission to install plugins.', 'fluent-cart')); |
| 390 |
} |
| 391 |
|
| 392 |
if ($pluginSlug === 'fluentcrm') { |
| 393 |
$pluginSlug = 'fluent-crm'; |
| 394 |
} |
| 395 |
|
| 396 |
$listedPlugins = apply_filters('fluent_cart/installable_repo_plugins', [ |
| 397 |
'fluent-crm', |
| 398 |
'fluent-smtp', |
| 399 |
'fluent-community', |
| 400 |
'fluent-security', |
| 401 |
'fluentform', |
| 402 |
'fluent-support' |
| 403 |
]); |
| 404 |
|
| 405 |
if (empty($pluginSlug) || !in_array($pluginSlug, $listedPlugins)) { |
| 406 |
return new \WP_Error('invalid_plugin', __('Invalid plugin selected for installation.', 'fluent-cart')); |
| 407 |
} |
| 408 |
|
| 409 |
$result = (new BackgroundInstaller())->installPlugin($pluginSlug); |
| 410 |
|
| 411 |
if (is_wp_error($result)) { |
| 412 |
return $result; |
| 413 |
} |
| 414 |
|
| 415 |
return [ |
| 416 |
'message' => __('The selected plugin has been installed. Please refresh the page.', 'fluent-cart') |
| 417 |
]; |
| 418 |
|
| 419 |
} |
| 420 |
|
| 421 |
public function changeStatus($request): array |
| 422 |
{ |
| 423 |
$feedId = intval(Arr::get($request, 'notification_id')); |
| 424 |
$status = Arr::get($request, 'status'); |
| 425 |
|
| 426 |
$feedQuery = Meta::query()->where('id', $feedId); |
| 427 |
$feed = $feedQuery->first(); |
| 428 |
$feedData = $feed['meta_value']; |
| 429 |
|
| 430 |
if (!$feed) { |
| 431 |
return [ |
| 432 |
'message' => __('Feed not found', 'fluent-cart') |
| 433 |
]; |
| 434 |
} |
| 435 |
|
| 436 |
$feedData['enabled'] = $status === 'yes' ? 'yes' : 'no'; |
| 437 |
|
| 438 |
$feedQuery->update([ |
| 439 |
//phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value |
| 440 |
'meta_value' => $feedData |
| 441 |
]); |
| 442 |
return [ |
| 443 |
'message' => __('Feed status updated', 'fluent-cart') |
| 444 |
]; |
| 445 |
|
| 446 |
} |
| 447 |
|
| 448 |
} |
| 449 |
|