Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
192 changes: 192 additions & 0 deletions backend/Actions/WCAffiliate/RecordApiHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
<?php

/**
* WC Affiliate Record API
*/

namespace BitApps\Integrations\Actions\WCAffiliate;

use BitApps\Integrations\Config;
use BitApps\Integrations\Core\Util\Common;
use BitApps\Integrations\Core\Util\Hooks;
use BitApps\Integrations\Log\LogHandler;

/**
* Provide functionality for WC Affiliate action execution.
*/
class RecordApiHelper
{
private $_integrationID;

private $_integrationDetails;

public function __construct($integrationDetails, $integId)
{
$this->_integrationDetails = $integrationDetails;
$this->_integrationID = $integId;
}

/**
* Execute the integration.
*
* @param array $fieldValues Field values from trigger.
* @param array $fieldMap Field mapping.
* @param array $utilities Utilities.
*
* @return array
*/
public function execute($fieldValues, $fieldMap, $utilities)
{
if (
!class_exists('\WC_Affiliate\Models\Affiliate')
|| !class_exists('\WC_Affiliate\Models\Referral')
|| !class_exists('\WC_Affiliate\Models\Transaction')
) {
return [
'success' => false,
'message' => __('WC Affiliate is not installed or activated', 'bit-integrations')
];
}

$fieldData = $this->generateReqDataFromFieldMap($fieldMap, $fieldValues);
$fieldData = $this->mergeFixedFieldValues($fieldData);
$mainAction = $this->_integrationDetails->mainAction ?? 'create_affiliate';

$defaultResponse = [
'success' => false,
// translators: %s: Plugin name.
'message' => wp_sprintf(
__('%s plugin is not installed or activated', 'bit-integrations'),
'Bit Integrations Pro'
)
];

switch ($mainAction) {
case 'create_affiliate':
$response = Hooks::apply(
Config::withPrefix('wcaffiliate_create_affiliate'),
$defaultResponse,
$fieldData
);
$type = 'affiliate';
$actionType = 'create_affiliate';

break;

case 'update_affiliate_status':
$response = Hooks::apply(
Config::withPrefix('wcaffiliate_update_affiliate_status'),
$defaultResponse,
$fieldData
);
$type = 'affiliate';
$actionType = 'update_affiliate_status';

break;

case 'create_referral':
$response = Hooks::apply(
Config::withPrefix('wcaffiliate_create_referral'),
$defaultResponse,
$fieldData
);
$type = 'referral';
$actionType = 'create_referral';

break;

case 'update_referral_status':
$response = Hooks::apply(
Config::withPrefix('wcaffiliate_update_referral_status'),
$defaultResponse,
$fieldData
);
$type = 'referral';
$actionType = 'update_referral_status';

break;

case 'create_transaction':
$response = Hooks::apply(
Config::withPrefix('wcaffiliate_create_transaction'),
$defaultResponse,
$fieldData
);
$type = 'transaction';
$actionType = 'create_transaction';

break;

case 'update_transaction_status':
$response = Hooks::apply(
Config::withPrefix('wcaffiliate_update_transaction_status'),
$defaultResponse,
$fieldData
);
$type = 'transaction';
$actionType = 'update_transaction_status';

break;

default:
$response = [
'success' => false,
'message' => __('Invalid action', 'bit-integrations')
];
$type = 'WCAffiliate';
$actionType = 'unknown';

break;
}

$responseType = isset($response['success']) && $response['success'] ? 'success' : 'error';

LogHandler::save(
$this->_integrationID,
['type' => $type, 'type_name' => $actionType],
$responseType,
$response
);

return $response;
}

private function generateReqDataFromFieldMap($fieldMap, $fieldValues)
{
$dataFinal = [];

foreach ($fieldMap as $item) {
$triggerValue = $item->formField;
$actionValue = $item->wcAffiliateField;

$dataFinal[$actionValue] = $triggerValue === 'custom' && isset($item->customValue)
? Common::replaceFieldWithValue($item->customValue, $fieldValues)
: ($fieldValues[$triggerValue] ?? '');
Comment on lines +162 to +164
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The logic for handling custom values is flawed. If triggerValue is set to 'custom' but customValue is missing, the code attempts to fetch a value from $fieldValues['custom']. Since 'custom' is a reserved keyword in the integration UI and not an actual field name, this will likely result in an empty string or incorrect data if a field named 'custom' happens to exist in the trigger. It should default to an empty string if it's a custom field but no value is provided.

            $dataFinal[$actionValue] = $triggerValue === 'custom'
                ? Common::replaceFieldWithValue($item->customValue ?? '', $fieldValues)
                : ($fieldValues[$triggerValue] ?? '');

}

return $dataFinal;
}

private function mergeFixedFieldValues($fieldData)
{
$fixedFieldValues = isset($this->_integrationDetails->fixedFieldValues)
? (array) $this->_integrationDetails->fixedFieldValues
: [];

if (empty($fixedFieldValues)) {
return $fieldData;
}

foreach ($fixedFieldValues as $key => $value) {
if (
!empty($key)
&& $value !== ''
&& (!isset($fieldData[$key]) || $fieldData[$key] === '')
) {
$fieldData[$key] = $value;
}
}

return $fieldData;
}
}
10 changes: 10 additions & 0 deletions backend/Actions/WCAffiliate/Routes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

if (!defined('ABSPATH')) {
exit;
}

use BitApps\Integrations\Actions\WCAffiliate\WCAffiliateController;
use BitApps\Integrations\Core\Util\Route;

Route::post('wc_affiliate_authorize', [WCAffiliateController::class, 'authorize']);
55 changes: 55 additions & 0 deletions backend/Actions/WCAffiliate/WCAffiliateController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/**
* WC Affiliate Integration
*/

namespace BitApps\Integrations\Actions\WCAffiliate;

use WP_Error;

/**
* Provide functionality for WC Affiliate integration.
*/
class WCAffiliateController
{
public static function authorize()
{
if (!self::isPluginReady()) {
wp_send_json_error(
__('WC Affiliate is not activated or not installed', 'bit-integrations'),
400
);
}

wp_send_json_success(true);
}

public function execute($integrationData, $fieldValues)
{
$integrationDetails = $integrationData->flow_details;
$integId = $integrationData->id;
$fieldMap = $integrationDetails->field_map;
$utilities = isset($integrationDetails->utilities) ? $integrationDetails->utilities : [];

if (empty($fieldMap)) {
return new WP_Error('field_map_empty', __('Field map is empty', 'bit-integrations'));
}

$recordApiHelper = new RecordApiHelper($integrationDetails, $integId);
$wcAffiliateResponse = $recordApiHelper->execute($fieldValues, $fieldMap, $utilities);

if (is_wp_error($wcAffiliateResponse)) {
return $wcAffiliateResponse;
}

return $wcAffiliateResponse;
}

private static function isPluginReady()
{
return class_exists('\WC_Affiliate\Models\Affiliate')
&& class_exists('\WC_Affiliate\Models\Referral')
&& class_exists('\WC_Affiliate\Models\Transaction');
}
}
1 change: 1 addition & 0 deletions backend/Core/Util/AllTriggersName.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public static function allTriggersName()
'EVF' => ['name' => 'Everest Forms', 'isPro' => true, 'is_active' => false],
'FormGent' => ['name' => 'FormGent', 'isPro' => true, 'is_active' => false],
'FluentAffiliate' => ['name' => 'FluentAffiliate', 'isPro' => true, 'is_active' => false],
'WCAffiliate' => ['name' => 'WC Affiliate', 'isPro' => true, 'is_active' => false],
'FF' => ['name' => 'Fluent Forms', 'isPro' => true, 'is_active' => false],
'FluentBoards' => ['name' => 'Fluent Boards', 'isPro' => true, 'is_active' => false],
'FluentBooking' => ['name' => 'Fluent Booking', 'isPro' => true, 'is_active' => false],
Expand Down
1 change: 1 addition & 0 deletions frontend/src/Utils/StaticData/webhookIntegrations.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export const customFormIntegrations = [
'WPTravelEngine',
'WPUserFrontend',
'FluentAffiliate',
'WCAffiliate',
'EasyCommerce',
'FormGent',
'GeoDirectory',
Expand Down
3 changes: 3 additions & 0 deletions frontend/src/components/AllIntegrations/EditInteg.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ const EditLine = lazy(() => import('./Line/EditLine'))
const EditACPT = lazy(() => import('./ACPT/EditACPT'))
const EditWishlistMember = lazy(() => import('./WishlistMember/EditWishlistMember'))
const EditFluentCart = lazy(() => import('./FluentCart/EditFluentCart'))
const EditWCAffiliate = lazy(() => import('./WCAffiliate/EditWCAffiliate'))
const EditWPCafe = lazy(() => import('./WPCafe/EditWPCafe'))
const EditNotificationX = lazy(() => import('./NotificationX/EditNotificationX'))
const EditTeamsForWooCommerceMemberships = lazy(() =>
Expand Down Expand Up @@ -583,6 +584,8 @@ const IntegType = memo(({ allIntegURL, flow }) => {
return <EditWishlistMember allIntegURL={allIntegURL} />
case 'FluentCart':
return <EditFluentCart allIntegURL={allIntegURL} />
case 'WC Affiliate':
return <EditWCAffiliate allIntegURL={allIntegURL} />
case 'WPCafe':
return <EditWPCafe allIntegURL={allIntegURL} />
case 'NotificationX':
Expand Down
3 changes: 3 additions & 0 deletions frontend/src/components/AllIntegrations/IntegInfo.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ const LineAuthorization = lazy(() => import('./Line/LineAuthorization'))
const ACPTAuthorization = lazy(() => import('./ACPT/ACPTAuthorization'))
const WishlistMemberAuthorization = lazy(() => import('./WishlistMember/WishlistMemberAuthorization'))
const FluentCartAuthorization = lazy(() => import('./FluentCart/FluentCartAuthorization'))
const WCAffiliateAuthorization = lazy(() => import('./WCAffiliate/WCAffiliateAuthorization'))
const WPCafeAuthorization = lazy(() => import('./WPCafe/WPCafeAuthorization'))
const TeamsForWooCommerceMembershipsAuthorization = lazy(
() => import('./TeamsForWooCommerceMemberships/TeamsForWooCommerceMembershipsAuthorization')
Expand Down Expand Up @@ -618,6 +619,8 @@ export default function IntegInfo() {
return <WishlistMemberAuthorization wishlistMemberConf={integrationConf} step={1} isInfo />
case 'FluentCart':
return <FluentCartAuthorization fluentCartConf={integrationConf} step={1} isInfo />
case 'WC Affiliate':
return <WCAffiliateAuthorization wcAffiliateConf={integrationConf} step={1} isInfo />
case 'WPCafe':
return <WPCafeAuthorization wpcafeConf={integrationConf} step={1} isInfo />
case 'Teams For WooCommerce Memberships':
Expand Down
10 changes: 10 additions & 0 deletions frontend/src/components/AllIntegrations/NewInteg.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ const Line = lazy(() => import('./Line/Line'))
const ACPT = lazy(() => import('./ACPT/ACPT'))
const WishlistMember = lazy(() => import('./WishlistMember/WishlistMember'))
const FluentCart = lazy(() => import('./FluentCart/FluentCart'))
const WCAffiliate = lazy(() => import('./WCAffiliate/WCAffiliate'))
const WPCafe = lazy(() => import('./WPCafe/WPCafe'))
const NotificationX = lazy(() => import('./NotificationX/NotificationX'))
const TeamsForWooCommerceMemberships = lazy(() =>
Expand Down Expand Up @@ -1644,6 +1645,15 @@ export default function NewInteg({ allIntegURL }) {
setFlow={setFlow}
/>
)
case 'WC Affiliate':
return (
<WCAffiliate
allIntegURL={allIntegURL}
formFields={flow?.triggerData?.fields}
flow={flow}
setFlow={setFlow}
/>
)
case 'WPCafe':
return (
<WPCafe
Expand Down
Loading
Loading