-
Notifications
You must be signed in to change notification settings - Fork 1
WC Affiliate Integration added #145
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
RishadAlam
wants to merge
4
commits into
main
Choose a base branch
from
feat/wc-affiliate
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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] ?? ''); | ||
| } | ||
|
|
||
| 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; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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']); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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'); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logic for handling custom values is flawed. If
triggerValueis set to'custom'butcustomValueis 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.