Skip to content

Commit

Permalink
Merge pull request #66 from thrivedesk/fix/wp-portal-issue
Browse files Browse the repository at this point in the history
fix: portal token verification issues
  • Loading branch information
shamsbd71 authored Nov 12, 2023
2 parents 51dd2f1 + 3295d1a commit 847b60a
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 50 deletions.
54 changes: 26 additions & 28 deletions includes/views/pages/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
$td_api_key = $td_helpdesk_selected_option['td_helpdesk_api_key'] ?? '';
$td_user_account_pages = get_option( 'td_user_account_pages' );
$td_selected_user_account_pages = $td_helpdesk_selected_option['td_user_account_pages'] ?? [];
$has_portal_access = ( new PortalService() )->has_portal_access();
$has_portal_access = ( new PortalService() )->has_portal_access() ?? '';
$wppostsync = WPPostSync::instance();

$td_user_account_pages = array(
Expand All @@ -34,7 +34,7 @@
?>

<div class="hidden tab-settings">
<form class="space-y-8" id="td_helpdesk_form" action="#" method="POST">
<form class="space-y-12" id="td_helpdesk_form" action="#" method="POST">
<!-- connection -->
<div class="space-y-1">
<div class="text-base font-bold"><?php _e( 'Connection Details', 'thrivedesk' ); ?></div>
Expand All @@ -60,8 +60,9 @@

<!-- assistant -->
<div class="space-y-1">
<div class="text-base font-bold"><?php _e( 'Assistant', 'thrivedesk' ); ?></div>
<p><?php _e('Add live chat assistant to your website', 'thrivedesk'); ?></p>
<div class="text-base font-bold"><?php _e('Assistant', 'thrivedesk'); ?></div>
<p><?php _e('Add live chat assistant to your website. To create your assistant click <a href="https://app.thrivedesk.com/assistants" target="_blank">
<span class="text-blue-500">here</span></a>', 'thrivedesk'); ?></p>
<div class="td-card">
<div class="space-y-2">
<label class="font-medium text-black text-sm"><?php _e( 'Select Assistant', 'thrivedesk' ); ?></label>
Expand All @@ -81,7 +82,7 @@
<div class="space-y-1">
<div class="text-base font-bold"><?php _e( 'WP Post Sync', 'thrivedesk' ); ?></div>
<p><?php _e( 'Sync your WordPress posts with ThriveDesk for faster support',
'thrivedesk' ); ?></p>
'thrivedesk'); ?></p>
<div class="td-card">
<div class="flex space-x-4" id="td_post_sync">
<div class="flex-1">
Expand All @@ -90,19 +91,18 @@
<?php if ($wppostsync && $wppostsync->get_plugin_data('connected')) : ?>
<?php foreach ( $wp_post_sync_types as $post_sync ) : ?>
<div>
<input class="td_helpdesk_post_sync" type="checkbox"
name="td_helpdesk_post_sync[]"
value="<?php echo esc_attr( $post_sync ); ?>" <?php echo in_array( $post_sync,
$td_selected_post_sync ) ? 'checked' : ''; ?>>
<label for="<?php echo esc_attr( $post_sync ); ?>"> <?php echo esc_html( ucfirst( $post_sync ) ); ?> </label>
<input class="td_helpdesk_post_sync" type="checkbox" name="td_helpdesk_post_sync[]" value="<?php echo esc_attr($post_sync); ?>" <?php echo in_array(
$post_sync,
$td_selected_post_sync
) ? 'checked' : ''; ?>>
<label for="<?php echo esc_attr($post_sync); ?>"> <?php echo esc_html(ucfirst($post_sync)); ?> </label>
</div>
<?php endforeach; ?>
<?php else: ?>
<?php else : ?>
<div class="w-full text-center text-base tab-link">
<?php _e('You need to install WordPress Post Sync app to get this feature', 'thrivedesk');?>
<?php $nonce = wp_create_nonce( 'thrivedesk-plugin-action' ); ?>
<a data-target="tab-integrations" href="#integrations"
class="btn-primary py-1 px-3">Connect Now</a>
<?php _e('You need to install WordPress Post Sync app to get this feature', 'thrivedesk'); ?>
<?php $nonce = wp_create_nonce('thrivedesk-plugin-action'); ?>
<a data-target="tab-integrations" href="#integrations" class="btn-primary py-1 px-3">Connect Now</a>
</div>
<?php endif; ?>
</div>
Expand All @@ -111,32 +111,30 @@ class="btn-primary py-1 px-3">Connect Now</a>
</div>
</div>
</div>

<!-- portal -->
<div class="space-y-1">
<div class="text-base font-bold"><?php _e( 'Portal', 'thrivedesk' ); ?></div>
<p><?php _e( 'Help center inside your website. Customer can create and reply tickets, access Knowledge base.',
'thrivedesk' ); ?></p>
<p><?php _e('Help center inside your website. Customer can create and reply tickets, access Knowledge base.','thrivedesk'); ?></p>
<div class="td-card">
<?php if (empty(get_td_helpdesk_options()['td_helpdesk_api_key'])): ?>
<div class="w-full text-center text-base" id="no_api_key_alert">
<?php _e('Please insert ThriveDesk API key above ☝️ to use the Portal feature inside your site.', 'thrivedesk');?>
</div>
<?php endif; ?>
<div class="text-center text-base" id="api_key_alert">
<?php _e('Please insert ThriveDesk API key above ☝️ to use the Portal feature inside your site.', 'thrivedesk'); ?>
</div>

<div class="alert alert-danger hidden" id="portal_feature">
<?php _e('Portal feature is available from the PRO plan and above. Please upgrade your subscription', 'thrivedesk');?>
<a class="text-blue-500" href="https://app.thrivedesk.com/billing/plans" target="_blank"><?php _e( 'here', 'thrivedesk' ); ?></a>.
<div class="alert alert-danger text-center <?php echo ($td_api_key && !$has_portal_access) ? '' : 'hidden' ?>" id="portal_feature">
<?php _e('Portal feature is available from the PRO plan. For plans details click', 'thrivedesk'); ?>
<a class="text-blue-500" href="https://app.thrivedesk.com/billing/plans" target="_blank"><?php _e('here', 'thrivedesk'); ?></a>.
</div>

<div class="flex space-x-4 <?php echo ($has_portal_access && !empty($td_api_key)) ? '' : 'hidden' ?>" id="td_post_content">
<div class="space-y-4 flex-1">
<div class="space-y-2">
<label for="td_helpdesk_page_id" class="font-medium text-black text-sm"><?php _e( 'New Ticket Page', 'thrivedesk' ); ?></label>
<label for="td_helpdesk_page_id" class="font-medium text-black text-sm"><?php _e('New Ticket Page', 'thrivedesk'); ?></label>
<select id="td_helpdesk_page_id" class="mt-1 bg-gray-50 border border-gray-300 rounded px-2 py-1 w-full max-w-full">
<option value=""> <?php _e( 'Select a page', 'thrivedesk' ); ?> </option>
<?php foreach ( get_pages() as $key => $page ) : ?>
<option value="<?php echo $page->ID; ?>" <?php echo array_key_exists( 'td_helpdesk_page_id',
$td_helpdesk_selected_option ) && $td_helpdesk_selected_option['td_helpdesk_page_id'] == $page->ID ? 'selected' : '' ?> >
$td_helpdesk_selected_option) && $td_helpdesk_selected_option['td_helpdesk_page_id'] == $page->ID ? 'selected' : '' ?>>
<?php echo $page->post_title; ?>
</option>
<?php endforeach; ?>
Expand Down Expand Up @@ -188,4 +186,4 @@ class="btn-primary py-1 px-3">Connect Now</a>
<?php _e( 'Save', 'thrivedesk' ); ?>
</button>
</form>
</div>
</div>
24 changes: 15 additions & 9 deletions resources/js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ jQuery(document).ready(($) => {
});
});

let $element = $('#td_helpdesk_api_key')[0].innerHTML;
if($element){
$('#api_key_alert').addClass('hidden');
}

// verify the API key
$('#td-api-verification-btn').on('click', async function (e) {
e.preventDefault();
Expand Down Expand Up @@ -220,16 +225,15 @@ jQuery(document).ready(($) => {
});
} else {
loadAssistants(apiKey);
isAllowedPortal()

$target.text('Verified');
$target.prop('disabled', true);

// remove the disabled attribute from the id td-assistants
$('#td-assistants').prop('disabled', false);
// add hidden class to the id td-api-verification-btn
$('#no_api_key_alert').addClass('hidden');

isAllowedPortal()
$('#api_key_alert').addClass('hidden');

Swal.fire({
icon: 'success',
Expand Down Expand Up @@ -313,12 +317,14 @@ jQuery(document).ready(($) => {
},
})
.success(function (response) {
let parsedResponse = JSON.parse(response);
let data = parsedResponse?.data;

if (data === true) {
$('#td_post_content').removeClass('hidden');
} else {
if(response.status === 'success'){
let parsedResponse = JSON.parse(response);
let data = parsedResponse?.data;
if (data === true) {
$('#td_post_content').removeClass('hidden');
}
}
else{
$('#portal_feature').removeClass('hidden');
}
})
Expand Down
24 changes: 12 additions & 12 deletions src/Conversations/Conversation.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public function __construct()

public function td_verify_helpdesk_api_key( ): void {
$apiKey = $_POST['data']['td_helpdesk_api_key'] ?? '';

if ( empty( $apiKey ) ) {
echo json_encode( [
'code' => 422,
Expand All @@ -75,15 +76,20 @@ public function td_verify_helpdesk_api_key( ): void {
die();
}

$data = get_transient( 'thrivedesk_me' );
if ( $data ) {
echo json_encode( [ 'status' => 'true', 'data' => $data ] );
die();
}

$apiService = new TDApiService();
$apiService->setApiKey( $apiKey );

$data = $apiService->getRequest( THRIVEDESK_API_URL . '/v1/me' );
if(!isset($data['company'])){
echo json_encode( [
'code' => 401,
'status' => 'error',
'data' => [
'message' => $data['message']
]
] );
die();
}

if ( isset( $data['wp_error'] ) && $data['wp_error'] ) {
echo json_encode( [
Expand All @@ -95,12 +101,6 @@ public function td_verify_helpdesk_api_key( ): void {
] );
die();
}

if ( $data ) {
set_transient( 'thrivedesk_me', $data, 60 * 60 * 6 );
echo json_encode( [ 'status' => 'true', 'data' => $data ] );
die();
}
}

public function td_save_helpdesk_form()
Expand Down
2 changes: 1 addition & 1 deletion thrivedesk.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ final class ThriveDesk
*
* @var string
*/
public $version = '1.1.1';
public $version = '1.1.2';

/**
* The single instance of this class
Expand Down

0 comments on commit 847b60a

Please sign in to comment.