Skip to content

Commit

Permalink
moved to js file
Browse files Browse the repository at this point in the history
  • Loading branch information
0xBeycan committed Dec 14, 2024
1 parent c4ffe8f commit 799fbe1
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 85 deletions.
58 changes: 58 additions & 0 deletions assets/js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,64 @@
let CryptoPayApp;
const __ = wp.i18n.__;


function jsonStringToObject(str) {
try {
return JSON.parse(str);
} catch (e) {
return null;
}
}

function getCustomPaymentDetails(details, method, data) {
const url = window.location.href;
const regex = /[?&]status=(\w+)/;
const match = url.match(regex);
const status = match ? match[1] : null;

if (data[method] !== undefined) {
if (DokanCryptoPay.key === method) {
let anotherDetails;
let network = jsonStringToObject(data[method].network);
let currency = jsonStringToObject(data[method].currency);
if (network && network) {
anotherDetails = `
<p>
<label>${__('Network:', 'cryptopay-withdrawal-for-dokan')}</label>
${network.name}
</p>
<p>
<label>${__('Currency:', 'cryptopay-withdrawal-for-dokan')}</label>
${currency.symbol}
</p>
<p>
<label>${__('Address:', 'cryptopay-withdrawal-for-dokan')}</label>
${data[method].address}
</p>
<br>
`;
} else {
anotherDetails = '';
}
details = status == 'pending' ? anotherDetails + `
<button title="${__('Pay with {title}', 'cryptopay-withdrawal-for-dokan').replace('{title}', DokanCryptoPay.title)}" class="button button-small pay-with-cryptopay" data-key="${DokanCryptoPay.key}" data-details='${JSON.stringify(data[method])}'>
${__('Pay with {title}', 'cryptopay-withdrawal-for-dokan').replace('{title}', DokanCryptoPay.title)}
</button>
` : anotherDetails;
}
}

return details;
}

dokan.hooks.addFilter(
'dokan_get_payment_details',
'getCustomPaymentDetails',
getCustomPaymentDetails,
33,
3
);

$(document).on('click', '.pay-with-cryptopay', async function() {
let key = $(this).data('key');
let parent = $(this).closest('tr');
Expand Down
79 changes: 0 additions & 79 deletions classes/DokanCryptoPayWithdrawal.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,6 @@ public function __construct(string $title, string $key)
add_filter('dokan_payment_settings_required_fields', [$this, 'addWithdrawInPaymentMethodList'], 10, 2);
add_filter('dokan_withdraw_method_additional_info', [$this, 'addWithdrawMethodAdditionalInfo'], 10, 2);

// Actions
// This a WordPress page detection
if (isset($_GET['page']) && 'dokan' == $_GET['page']) {
add_action('admin_print_footer_scripts', [$this, 'withdrawDetails'], 99);
}

if ('dokan_cryptopay' == $this->key) {
Helpers::registerIntegration($this->key);
Hook::addFilter('mode_' . $this->key, fn () => 'network');
Expand Down Expand Up @@ -328,79 +322,6 @@ public function addWithdrawMethodAdditionalInfo(string $methodInfo, string $meth
return $methodInfo;
}

/**
* @return void
*/
public function withdrawDetails(): void
{
?>
<script>
function jsonStringToObject(str) {
try {
return JSON.parse(str);
} catch (e) {
return null;
}
}

function getCustomPaymentDetails(details, method, data) {
const url = window.location.href;
const regex = /[?&]status=(\w+)/;
const match = url.match(regex);
const status = match ? match[1] : null;

if (data[method] !== undefined) {
if ('<?php echo esc_js($this->key) ?>' === method) {
let anotherDetails;
let network = jsonStringToObject(data[method].network);
let currency = jsonStringToObject(data[method].currency);
if (network && network) {
anotherDetails = `
<p>
<label><?php echo esc_html__('Network:', 'cryptopay-withdrawal-for-dokan'); ?></label>
${network.name}
</p>
<p>
<label><?php echo esc_html__('Currency:', 'cryptopay-withdrawal-for-dokan'); ?></label>
${currency.symbol}
</p>
<p>
<label><?php echo esc_html__('Address:', 'cryptopay-withdrawal-for-dokan'); ?></label>
${data[method].address}
</p>
<br>
`;
} else {
anotherDetails = '';
}
details = status == 'pending' ? anotherDetails + `
<button title="<?php
/* translators: %s: payment method title */
echo esc_attr(sprintf(__('Pay with %s', 'cryptopay-withdrawal-for-dokan'), $this->title))
?>" class="button button-small pay-with-cryptopay" data-key="<?php echo esc_attr($this->key); ?>" data-details='${JSON.stringify(data[method])}'>
<?php
// translators: %s: payment method title
echo esc_html(sprintf(__('Pay with %s', 'cryptopay-withdrawal-for-dokan'), $this->title))
?>
</button>
` : anotherDetails;
}
}

return details;
}

dokan.hooks.addFilter(
'dokan_get_payment_details',
'getCustomPaymentDetails',
getCustomPaymentDetails,
33,
3
);
</script>
<?php
}

/**
* @return string
*/
Expand Down
8 changes: 5 additions & 3 deletions cryptopay-withdrawal-for-dokan.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

/**
* Plugin Name: CryptoPay Withdrawal for Dokan
* Version: 1.0.6
* Version: 1.0.7
* Plugin URI: https://beycanpress.com/cryptopay/
* Description: Add custom cryptocurrency withdrawal method to Dokan plugin
* Author: BeycanPress LLC
Expand All @@ -30,7 +30,7 @@
use BeycanPress\CryptoPayLite\Loader as LiteLoader;

define('DOKAN_CRYPTOPAY_FILE', __FILE__);
define('DOKAN_CRYPTOPAY_VERSION', '1.0.6');
define('DOKAN_CRYPTOPAY_VERSION', '1.0.7');
define('DOKAN_CRYPTOPAY_URL', plugin_dir_url(__FILE__));
define('DOKAN_CRYPTOPAY_PATH', plugin_dir_path(__FILE__));

Expand Down Expand Up @@ -72,7 +72,9 @@
wp_enqueue_script('dokan-cryptopay', plugin_dir_url(__FILE__) . 'assets/js/admin.js', ['jquery', 'wp-i18n'], DOKAN_CRYPTOPAY_VERSION, true);
wp_localize_script('dokan-cryptopay', 'DokanCryptoPay', [
'currency' => get_woocommerce_currency(),
'apiUrl' => home_url('/wp-json/dokan/v1/withdraw/')
'apiUrl' => home_url('/wp-json/dokan/v1/withdraw/'),
'key' => class_exists(Loader::class) ? 'dokan_cryptopay' : 'dokan_cryptopay_lite',
'title' => class_exists(Loader::class) ? esc_html__('CryptoPay', 'cryptopay-withdrawal-for-dokan') : esc_html__('CryptoPay Lite', 'cryptopay-withdrawal-for-dokan'),
]);
});
add_action('admin_footer', function () use ($gateway): void {
Expand Down
6 changes: 3 additions & 3 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ Tags: Bitcoin, Ethereum, Cryptocurrency, Payments, Dokan
Requires at least: 5.0
Tested up to: 6.7.1
Requires PHP: 8.1
Stable Tag: 1.0.6
Version: 1.0.6
Stable Tag: 1.0.7
Version: 1.0.7
License: GPLv3
License URI: https://www.gnu.org/licenses/gpl-3.0.html

Expand Down Expand Up @@ -44,7 +44,7 @@ You can easily translate with Loco translate.

== Changelog ==

= 1.0.6 =
= 1.0.7 =
* Fixed: Configure for new versions

= 1.0.5 =
Expand Down

0 comments on commit 799fbe1

Please sign in to comment.