Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
124 changes: 124 additions & 0 deletions backend/Actions/Instasent/InstasentController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

/**
* Instasent Integration
*/

namespace BitApps\Integrations\Actions\Instasent;

use BitApps\Integrations\Core\Util\HttpHelper;
use WP_Error;

/**
* Provide functionality for Instasent integration
*/
class InstasentController
{
private static $_baseUrl = 'https://api.instasent.com';

public function authorize($refreshFieldsRequestParams)
{
if (empty($refreshFieldsRequestParams->auth_token)) {

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

Accessing properties on $refreshFieldsRequestParams when it is null or not an object will trigger a fatal error in PHP 8.0+. Guard against null/empty request parameters before accessing properties.

        if (empty($refreshFieldsRequestParams) || empty($refreshFieldsRequestParams->auth_token)) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done — the guard now checks the request object itself before accessing properties: empty($refreshFieldsRequestParams) || empty($refreshFieldsRequestParams->auth_token).

wp_send_json_error(
__(
'Requested parameter is empty',
'bit-integrations'
),
400
);
}

$endpoint = self::$_baseUrl . '/organization/account';
$header = [
'Authorization' => 'Bearer ' . $refreshFieldsRequestParams->auth_token,
'Content-Type' => 'application/json',
'Accept' => 'application/json',
];

$response = HttpHelper::get($endpoint, null, $header);

if (HttpHelper::$responseCode == 200) {
Comment on lines +38 to +44

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

Always validate the response with is_wp_error() before checking response codes or accessing properties to prevent fatal errors in PHP 8.0+.

        $response = HttpHelper::get($endpoint, null, $header);

        if (is_wp_error($response)) {
            wp_send_json_error($response->get_error_message(), 400);
        }

        if (HttpHelper::$responseCode == 200) {
References
  1. In PHP, when handling responses that may return a WP_Error object, always use the is_wp_error() function to validate the response before accessing it as an array or object. This prevents fatal errors in PHP 8.0+.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done — added an is_wp_error($response) guard right after the request, before reading the response code/properties.

wp_send_json_success('Authorization Successful', 200);

return;
}

wp_send_json_error(
$response->message ?? $response ?? 'Authorization Failed',
400
);
}

public function refreshDatasources($refreshFieldsRequestParams)
{
if (empty($refreshFieldsRequestParams->auth_token) || empty($refreshFieldsRequestParams->projectId)) {

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

Accessing properties on $refreshFieldsRequestParams when it is null or not an object will trigger a fatal error in PHP 8.0+. Guard against null/empty request parameters before accessing properties.

        if (empty($refreshFieldsRequestParams) || empty($refreshFieldsRequestParams->auth_token) || empty($refreshFieldsRequestParams->projectId)) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done — the guard now checks the request object itself before accessing properties: empty($refreshFieldsRequestParams) || empty($refreshFieldsRequestParams->auth_token).

wp_send_json_error(
__('Requested parameter is empty', 'bit-integrations'),
400
);
}

$project = rawurlencode($refreshFieldsRequestParams->projectId);
$endpoint = self::$_baseUrl . "/v1/project/{$project}/datasource";
$header = [
'Authorization' => 'Bearer ' . $refreshFieldsRequestParams->auth_token,
'Content-Type' => 'application/json',
'Accept' => 'application/json',
];

$response = HttpHelper::get($endpoint, null, $header);

if (HttpHelper::$responseCode == 200) {
Comment on lines +73 to +79

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

Always validate the response with is_wp_error() before checking response codes or accessing properties to prevent fatal errors in PHP 8.0+.

        $response = HttpHelper::get($endpoint, null, $header);

        if (is_wp_error($response)) {
            wp_send_json_error($response->get_error_message(), 400);
        }

        if (HttpHelper::$responseCode == 200) {
References
  1. In PHP, when handling responses that may return a WP_Error object, always use the is_wp_error() function to validate the response before accessing it as an array or object. This prevents fatal errors in PHP 8.0+.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done — added an is_wp_error($response) guard right after the request, before reading the response code/properties.

$entities = $response->entities ?? [];
$formattedResponse = [];
foreach ($entities as $entity) {
$formattedResponse[] = [
'id' => $entity->id ?? '',
'name' => $entity->name ?? ($entity->id ?? ''),
];
}

wp_send_json_success($formattedResponse, 200);

return;
}

wp_send_json_error(
$response->message ?? $response ?? __('Failed to fetch data sources', 'bit-integrations'),
400
);
}

public function execute($integrationData, $fieldValues)
{
$integrationDetails = $integrationData->flow_details;
$integId = $integrationData->id;
$auth_token = $integrationDetails->auth_token ?? '';
$fieldMap = $integrationDetails->field_map ?? '';
$action = $integrationDetails->action ?? '';

if (
empty($fieldMap)
|| empty($auth_token)
|| empty($action)
) {
// translators: %s: Placeholder value
return new WP_Error('REQ_FIELD_EMPTY', wp_sprintf(__('module, fields are required for %s api', 'bit-integrations'), 'Instasent'));
}

$recordApiHelper = new RecordApiHelper($auth_token, $integrationDetails, $integId);
$instasentApiResponse = $recordApiHelper->execute(
$integrationDetails,
$fieldValues,
$fieldMap,
$auth_token,
$action
);

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

return $instasentApiResponse;

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 is_wp_error check here is redundant because both branches return $instasentApiResponse. Simplify the code by directly returning the response.

        return $instasentApiResponse;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done — removed the redundant is_wp_error branch; the method now returns $instasentApiResponse directly.

}
}
126 changes: 126 additions & 0 deletions backend/Actions/Instasent/RecordApiHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php

/**
* Instasent Record Api
*/

namespace BitApps\Integrations\Actions\Instasent;

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

/**
* Provide functionality for Record insert, upsert
*/
class RecordApiHelper
{
private $_integrationID;

private $_integrationDetails;

private $_authToken;

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

public function generateReqDataFromFieldMap($data, $fieldMap)
{
$dataFinal = [];

foreach ($fieldMap as $key => $value) {
$triggerValue = $value->formField;
$actionValue = $value->instasentFormField;

if (empty($actionValue)) {
continue;
}

if ($triggerValue === 'custom' && isset($value->customValue)) {
$dataFinal[$actionValue] = Common::replaceFieldWithValue($value->customValue, $data);
} elseif (isset($data[$triggerValue])) {
$dataFinal[$actionValue] = $data[$triggerValue];
}
}

return $dataFinal;
}

public function execute(
$integrationDetails,
$fieldValues,
$fieldMap,
$auth_token,
$action
) {
$fieldData = $this->generateReqDataFromFieldMap($fieldValues, $fieldMap);

$default = [
'success' => false,
'message' => __('Bit Integrations Pro is required.', 'bit-integrations'),
];

switch ($action) {
case 'send_sms':
$apiResponse = Hooks::apply(Config::withPrefix('instasent_send_sms'), $default, $fieldData, $auth_token);
$typeName = 'send-sms';

break;

case 'create_lookup':
$apiResponse = Hooks::apply(Config::withPrefix('instasent_create_lookup'), $default, $fieldData, $auth_token);
$typeName = 'create-lookup';

break;

case 'create_datasource':
$apiResponse = Hooks::apply(Config::withPrefix('instasent_create_datasource'), $default, $fieldData, $integrationDetails, $auth_token);
$typeName = 'create-datasource';

break;

case 'create_or_update_contact':
$apiResponse = Hooks::apply(Config::withPrefix('instasent_create_or_update_contact'), $default, $fieldData, $integrationDetails, $auth_token);
$typeName = 'create-or-update-contact';

break;

case 'delete_contact':
$apiResponse = Hooks::apply(Config::withPrefix('instasent_delete_contact'), $default, $fieldData, $integrationDetails, $auth_token);
$typeName = 'delete-contact';

break;

case 'create_contact_event':
$apiResponse = Hooks::apply(Config::withPrefix('instasent_create_contact_event'), $default, $fieldData, $integrationDetails, $auth_token);
$typeName = 'create-contact-event';

break;

default:
$apiResponse = $default;
$typeName = $action;

break;
}

$apiResponse = \is_array($apiResponse) ? (object) $apiResponse : $apiResponse;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

Always validate the response with is_wp_error() before casting or accessing properties to prevent fatal errors in PHP 8.0+.

        if (is_wp_error($apiResponse)) {
            LogHandler::save(
                $this->_integrationID,
                wp_json_encode(['type' => 'action', 'type_name' => $typeName]),
                'error',
                wp_json_encode(['message' => $apiResponse->get_error_message()])
            );

            return $apiResponse;
        }

        $apiResponse = \is_array($apiResponse) ? (object) $apiResponse : $apiResponse;
References
  1. In PHP, when handling responses that may return a WP_Error object, always use the is_wp_error() function to validate the response before accessing it as an array or object. This prevents fatal errors in PHP 8.0+.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done — added an is_wp_error() guard (with an error log) before casting $apiResponse.


if (
(isset($apiResponse->success) && $apiResponse->success)
|| isset($apiResponse->id)
|| isset($apiResponse->data)
) {
LogHandler::save($this->_integrationID, wp_json_encode(['type' => 'action', 'type_name' => $typeName]), 'success', wp_json_encode($apiResponse));
} else {
LogHandler::save($this->_integrationID, wp_json_encode(['type' => 'action', 'type_name' => $typeName]), 'error', wp_json_encode($apiResponse));
}

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

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

use BitApps\Integrations\Actions\Instasent\InstasentController;
use BitApps\Integrations\Core\Util\Route;

Route::post('instasent_authorize', [InstasentController::class, 'authorize']);
Route::post('refresh_instasent_datasources', [InstasentController::class, 'refreshDatasources']);
3 changes: 3 additions & 0 deletions frontend/src/Utils/StaticData/tutorialLinks.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,9 @@ const tutorialLinks = {
youTubeLink: 'https://www.youtube.com/playlist?list=PL7c6CDwwm-AKe60pZMUmlWnHQWKrx8nw8',
docLink: 'https://bit-integrations.com/wp-docs/actions/mailerlite-integrations/'
},
instasent: {
docLink: '#'
},
mailchimp: {
youTubeLink: 'https://www.youtube.com/playlist?list=PL7c6CDwwm-ALUaeqiK9GwBSxVkAod1PzP',
docLink: 'https://bit-integrations.com/wp-docs/actions/mailchimp-integrations/'
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 @@ -68,6 +68,7 @@ const EditAcumbamail = lazy(() => import('./Acumbamail/EditAcumbamail'))
const EditGroundhogg = lazy(() => import('./Groundhogg/EditGroundhogg'))
const EditSendFox = lazy(() => import('./SendFox/EditSendFox'))
const EditMailerLite = lazy(() => import('./MailerLite/EditMailerLite'))
const EditInstasent = lazy(() => import('./Instasent/EditInstasent'))
const EditVbout = lazy(() => import('./Vbout/EditVbout'))
const EditWhatsApp = lazy(() => import('./WhatsApp/EditWhatsApp'))
const EditLearnDash = lazy(() => import('./LearnDash/EditLearnDash'))
Expand Down Expand Up @@ -397,6 +398,8 @@ const IntegType = memo(({ allIntegURL, flow }) => {
return <EditSendFox allIntegURL={allIntegURL} />
case 'MailerLite':
return <EditMailerLite allIntegURL={allIntegURL} />
case 'Instasent':
return <EditInstasent allIntegURL={allIntegURL} />
case 'Vbout':
return <EditVbout allIntegURL={allIntegURL} />
case 'WhatsApp':
Expand Down
Loading
Loading