| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Http\Controllers; |
| 4 |
|
| 5 |
use FluentCart\App\Models\ProductMeta; |
| 6 |
use FluentCart\App\Models\Product; |
| 7 |
use FluentCart\App\Modules\Integrations\GlobalIntegrationSettings; |
| 8 |
use FluentCart\App\Modules\Integrations\IntegrationHelper; |
| 9 |
use FluentCart\Framework\Http\Controller; |
| 10 |
use FluentCart\Framework\Http\Request\Request; |
| 11 |
use FluentCart\Framework\Support\Arr; |
| 12 |
|
| 13 |
class ProductIntegrationsController extends Controller |
| 14 |
{ |
| 15 |
public function getFeeds(Request $request) |
| 16 |
{ |
| 17 |
$formattedFeeds = $this->getNotificationFeeds($request->productId); |
| 18 |
$availableIntegrations = apply_filters('fluent_cart/integration/order_integrations', []); |
| 19 |
|
| 20 |
$availableIntegrations = array_filter($availableIntegrations, function ($integration) { |
| 21 |
return in_array('product', Arr::get($integration, 'scopes', [])) && $integration['enabled']; |
| 22 |
}); |
| 23 |
|
| 24 |
$integrationKeys = array_keys($availableIntegrations); |
| 25 |
|
| 26 |
$formattedFeeds = array_filter($formattedFeeds, function ($feed) use ($integrationKeys) { |
| 27 |
return in_array($feed['provider'], $integrationKeys); |
| 28 |
}); |
| 29 |
|
| 30 |
return [ |
| 31 |
'feeds' => array_values($formattedFeeds), |
| 32 |
'available_integrations' => $availableIntegrations, |
| 33 |
'all_module_config_url' => admin_url('admin.php?page=fluent-cart#/integrations') |
| 34 |
]; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Get product-specific integration settings |
| 39 |
* |
| 40 |
* @param \FluentCart\Framework\Http\Request\Request $request |
| 41 |
* @param int $product_id |
| 42 |
* @param string $integration_name |
| 43 |
*/ |
| 44 |
public function getProductIntegrationSettings(Request $request, $product_id, $integration_name) |
| 45 |
{ |
| 46 |
$product = Product::findOrFail($product_id); |
| 47 |
|
| 48 |
$allIntegrations = apply_filters('fluent_cart/integration/order_integrations', []); |
| 49 |
|
| 50 |
if (!isset($allIntegrations[$integration_name])) { |
| 51 |
return $this->sendError([ |
| 52 |
'message' => __('Integration not found', 'fluent-cart') |
| 53 |
]); |
| 54 |
} |
| 55 |
|
| 56 |
$baseAddon = $allIntegrations[$integration_name]; |
| 57 |
if (!in_array('product', Arr::get($baseAddon, 'scopes', [])) || !$baseAddon['enabled']) { |
| 58 |
return $this->sendError([ |
| 59 |
'message' => __('This integration is not available for products or not enabled', 'fluent-cart') |
| 60 |
]); |
| 61 |
} |
| 62 |
|
| 63 |
// This is just for validation, we will use the integration_id if provided |
| 64 |
$integrationId = $request->get('integration_id', 0); |
| 65 |
if ($integrationId) { |
| 66 |
$integrationMeta = ProductMeta::where('id', $integrationId) |
| 67 |
->where('object_id', $product->ID) |
| 68 |
->where('object_type', 'product_integration') |
| 69 |
->first(); |
| 70 |
|
| 71 |
if (!$integrationMeta) { |
| 72 |
return $this->sendError([ |
| 73 |
'message' => __('Integration not found', 'fluent-cart') |
| 74 |
]); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
$integrationManager = new GlobalIntegrationSettings(); |
| 79 |
$settings = $integrationManager->getIntegrationSettings( |
| 80 |
[ |
| 81 |
'integration_name' => $integration_name, |
| 82 |
'integration_id' => $this->request->get('integration_id', 0), |
| 83 |
'product_id' => $product_id |
| 84 |
], |
| 85 |
'product' |
| 86 |
); |
| 87 |
|
| 88 |
$settings['product_variations'] = $product->variants() |
| 89 |
->select('id', 'variation_title') |
| 90 |
->get() |
| 91 |
->map(function ($variation) { |
| 92 |
return [ |
| 93 |
'id' => $variation->id, |
| 94 |
'title' => $variation->variation_title |
| 95 |
]; |
| 96 |
})->toArray(); |
| 97 |
|
| 98 |
if (empty($settings['settings']['conditional_variation_ids'])) { |
| 99 |
$settings['settings']['conditional_variation_ids'] = []; |
| 100 |
} |
| 101 |
|
| 102 |
$settings['scope'] = 'product'; |
| 103 |
|
| 104 |
return $settings; |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
/** |
| 109 |
* @param \FluentCart\Framework\Http\Request\Request $request |
| 110 |
* @param $product_id |
| 111 |
* @return array|\WP_REST_Response |
| 112 |
*/ |
| 113 |
public function saveProductIntegration(Request $request, $product_id) |
| 114 |
{ |
| 115 |
$product = Product::find($product_id); |
| 116 |
if (!$product) { |
| 117 |
return $this->sendError([ |
| 118 |
'message' => __('Product not found', 'fluent-cart') |
| 119 |
]); |
| 120 |
} |
| 121 |
|
| 122 |
$requestData = $request->all(); |
| 123 |
$integrationId = Arr::get($requestData, 'integration_id'); |
| 124 |
$integrationMeta = null; |
| 125 |
if ($integrationId) { |
| 126 |
$integrationMeta = ProductMeta::where('id', $integrationId) |
| 127 |
->where('object_id', $product_id) |
| 128 |
->where('object_type', 'product_integration') |
| 129 |
->first(); |
| 130 |
|
| 131 |
if (!$integrationMeta) { |
| 132 |
return $this->sendError([ |
| 133 |
'message' => __('Integration not found', 'fluent-cart') |
| 134 |
]); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
$integrationFeed = json_decode(Arr::get($requestData, 'integration'), true); |
| 139 |
$provider = Arr::get($requestData, 'integration_name'); |
| 140 |
|
| 141 |
$integrationFeedData = IntegrationHelper::validateAndFormatIntegrationFeedSettings($integrationFeed, [ |
| 142 |
'provider' => $provider, |
| 143 |
'scope' => 'product', |
| 144 |
'product_id' => $product_id, |
| 145 |
'integration_id' => $integrationId |
| 146 |
]); |
| 147 |
|
| 148 |
if (is_wp_error($integrationFeedData)) { |
| 149 |
return $this->sendError([ |
| 150 |
'message' => $integrationFeedData->get_error_message(), |
| 151 |
'errors' => $integrationFeedData->get_error_data() |
| 152 |
]); |
| 153 |
} |
| 154 |
|
| 155 |
if ($integrationMeta) { |
| 156 |
$integrationMeta->meta_value = $integrationFeedData; |
| 157 |
$integrationMeta->save(); |
| 158 |
} else { |
| 159 |
$integrationMeta = ProductMeta::create([ |
| 160 |
'object_id' => $product_id, |
| 161 |
'object_type' => 'product_integration', |
| 162 |
//phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key |
| 163 |
'meta_key' => $provider, |
| 164 |
//phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value |
| 165 |
'meta_value' => $integrationFeedData |
| 166 |
]); |
| 167 |
} |
| 168 |
|
| 169 |
do_action('fluent_cart/reindex_integration_feeds', []); |
| 170 |
|
| 171 |
return [ |
| 172 |
'message' => __('Integration has been successfully saved', 'fluent-cart'), |
| 173 |
'integration_id' => $integrationId, |
| 174 |
'integration_name' => $provider, |
| 175 |
'created' => $integrationMeta->wasRecentlyCreated, |
| 176 |
'feedData' => $integrationFeedData |
| 177 |
]; |
| 178 |
} |
| 179 |
|
| 180 |
public function changeStatus(Request $request) |
| 181 |
{ |
| 182 |
$productId = $request->get('product_id'); |
| 183 |
$notificationId = $request->get('notification_id'); |
| 184 |
|
| 185 |
if (!$productId || !$notificationId) { |
| 186 |
return $this->sendError([ |
| 187 |
'message' => __('Product ID and Notification ID are required', 'fluent-cart') |
| 188 |
]); |
| 189 |
} |
| 190 |
|
| 191 |
$product = Product::findOrFail($productId); |
| 192 |
$notification = ProductMeta::where('id', $notificationId) |
| 193 |
->where('object_id', $product->ID) |
| 194 |
->where('object_type', 'product_integration') |
| 195 |
->first(); |
| 196 |
|
| 197 |
if (!$notification) { |
| 198 |
return $this->sendError([ |
| 199 |
'message' => __('Notification not found', 'fluent-cart') |
| 200 |
]); |
| 201 |
} |
| 202 |
|
| 203 |
$metaValue = $notification->meta_value; |
| 204 |
|
| 205 |
$metaValue['enabled'] = $request->get('status') === 'yes' ? 'yes' : 'no'; |
| 206 |
$notification->meta_value = $metaValue; |
| 207 |
$notification->save(); |
| 208 |
|
| 209 |
return [ |
| 210 |
'message' => __('Integration status has been updated', 'fluent-cart') |
| 211 |
]; |
| 212 |
} |
| 213 |
|
| 214 |
public function deleteProductIntegration(Request $request, $product_id, $integration_id) |
| 215 |
{ |
| 216 |
// Verify product exists |
| 217 |
$product = Product::findOrFail($product_id); |
| 218 |
|
| 219 |
// Delete integration |
| 220 |
ProductMeta::where('id', $integration_id) |
| 221 |
->where('object_id', $product->ID) |
| 222 |
->where('object_type', 'product_integration') |
| 223 |
->delete(); |
| 224 |
|
| 225 |
return [ |
| 226 |
'message' => __('Integration deleted successfully', 'fluent-cart') |
| 227 |
]; |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* Get integration settings for a specific integration type |
| 232 |
* |
| 233 |
* @param string $integration_type |
| 234 |
*/ |
| 235 |
private function getIntegrationSettings($integration_type) |
| 236 |
{ |
| 237 |
$integrationManager = new GlobalIntegrationSettings(); |
| 238 |
return $integrationManager->getIntegrationSettings( |
| 239 |
[ |
| 240 |
'integration_name' => $integration_type |
| 241 |
], |
| 242 |
'product' |
| 243 |
); |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
private function getNotificationFeeds($productId) |
| 248 |
{ |
| 249 |
$feeds = ProductMeta::query() |
| 250 |
->where('object_id', $productId) |
| 251 |
->where('object_type', 'product_integration') |
| 252 |
->orderBy('id', 'ASC') |
| 253 |
->get(); |
| 254 |
|
| 255 |
$formattedFeeds = []; |
| 256 |
|
| 257 |
foreach ($feeds as $feed) { |
| 258 |
$data = $feed->meta_value; |
| 259 |
$formattedFeeds[] = [ |
| 260 |
'id' => $feed->id, |
| 261 |
'name' => Arr::get($data, 'name'), |
| 262 |
'enabled' => $data['enabled'], |
| 263 |
'provider' => $feed->meta_key, |
| 264 |
'feed' => $data, |
| 265 |
'scope' => 'product', |
| 266 |
]; |
| 267 |
} |
| 268 |
|
| 269 |
return $formattedFeeds; |
| 270 |
} |
| 271 |
|
| 272 |
} |
| 273 |
|