Skip to content

Commit 78e3a6d

Browse files
committed
feat: added media selector for QR code
1 parent d29d021 commit 78e3a6d

File tree

8 files changed

+118
-8
lines changed

8 files changed

+118
-8
lines changed

assets/admin.css

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
.jp-gcash-upload-fields button {
2+
width: 100px;
3+
}
4+
5+
#prev_url_gcash_jp {
6+
display: block;
7+
width: 100%;
8+
margin-bottom: 8px;
9+
}
10+
11+
.jp-gcash-upload-fields img {
12+
width: 100px;
13+
height: auto;
14+
max-width: none;
15+
max-height: none;
16+
}

assets/admin.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
jQuery(function($) {
2+
3+
// Initialize variables
4+
var custom_uploader;
5+
var input = '#woocommerce_jp_gcash_manual_gcash_qr';
6+
var buttonText = 'Select QR';
7+
8+
// Ensure the input element exists before continuing
9+
if ($(input).length) {
10+
11+
var selected = $(input).val();
12+
13+
$(input).wrap('<div class="jp-gcash-upload-fields">');
14+
$("<span id='prev_url_gcash_jp'></span>").insertBefore(input);
15+
16+
if (selected) {
17+
buttonText = "Update QR";
18+
$('#prev_url_gcash_jp').html('Loading preview..')
19+
wp.media.attachment(selected).fetch().then(function (data) {
20+
$('#prev_url_gcash_jp').html('<img src="' + wp.media.attachment(selected).get('url') + '" />');
21+
});
22+
}
23+
24+
$("<button type='button' class='button media-button select-mode-toggle-button' id='woocommerce_jp_gcash_manual_gcash_qr_select'>" + buttonText + "</button>")
25+
.insertBefore(input);
26+
}
27+
28+
// Event listener for the select button
29+
$('body').on('click', '#woocommerce_jp_gcash_manual_gcash_qr_select', function(e) {
30+
e.preventDefault();
31+
32+
// Open the media uploader or create it if it doesn't exist
33+
if (custom_uploader) {
34+
custom_uploader.open();
35+
return;
36+
}
37+
38+
custom_uploader = wp.media.frames.file_frame = wp.media({
39+
title: 'Choose Image',
40+
button: { text: 'Choose Image' },
41+
multiple: false // Assuming only one image can be selected
42+
});
43+
44+
// Handle the selection of an image
45+
custom_uploader.on('select', function() {
46+
var attachment = custom_uploader.state().get('selection').first().toJSON();
47+
$(input).val(attachment.id).trigger('change');
48+
$('#woocommerce_jp_gcash_manual_gcash_qr_preview').hide();
49+
$('#prev_url_gcash_jp').html('<img src="' + attachment.url + '" />');
50+
});
51+
52+
custom_uploader.open();
53+
});
54+
55+
});

assets/admin.min.css

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assets/admin.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

includes/payment.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ public function init_form_fields() {
7171
'desc_tip' => false,
7272
),
7373
'gcash_qr' => array(
74-
'title' => __( 'Gcash QR code image link', 'gcash-payment-gateway-for-woocommerce' ),
75-
'type' => 'url',
76-
'description' => __( 'Link to your Gcash QR Code image. <a href="https://help.gcash.com/hc/en-us/articles/15725514628121-Generate-your-Digital-QR-via-GCashPro-Portal" target="_blank">How to get my QR code</a>.', 'gcash-payment-gateway-for-woocommerce' ),
74+
'title' => __( 'Gcash QR code image', 'gcash-payment-gateway-for-woocommerce' ),
75+
'type' => 'hidden',
76+
'description' => __( 'Upload your Gcash QR Code image. <a href="https://help.gcash.com/hc/en-us/articles/15725514628121-Generate-your-Digital-QR-via-GCashPro-Portal" target="_blank">How to get my QR code</a>.', 'gcash-payment-gateway-for-woocommerce' ),
7777
'default' => '',
7878
'required' => true
7979
),
@@ -115,6 +115,6 @@ public function process_payment( $order_id ) {
115115
'redirect' => $this->get_return_url( $order ),
116116
);
117117
}
118-
118+
119119
}
120120
}, 11 );

includes/reference.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ public function ref_field( $description, $payment_id )
4646
<p class="jp-manual-gcash-checkout-instruction"><?php echo wp_kses_post( $ins ) ?></p>
4747
<?php if ( $qr ) { ?>
4848
<div class="jp-manual-gcash-qr-wrapper">
49-
<img src="<?php echo esc_url( $qr ) ?>" alt="merchant gcash qr code" class="jp-manual-gcash-qr" />
49+
<?php
50+
echo wp_get_attachment_image( $qr, 'full' );
51+
?>
5052
</div>
5153
<?php
5254
} else if ( current_user_can( 'administrator' ) ) {
@@ -95,7 +97,7 @@ public function inline_styles( $description, $payment_id )
9597
.payment_box.payment_method_jp_gcash_manual {
9698
padding: 18px !important;
9799
}
98-
.jp-manual-gcash-qr {
100+
.jp-manual-gcash-qr-wrapper img {
99101
float: none !important;
100102
width: 100% !important;
101103
padding: 18px !important;

includes/upload.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
if ( ! defined( 'ABSPATH' ) ) {
4+
exit; // Exit if accessed directly
5+
}
6+
7+
class JP_Manual_Gcash_Upload {
8+
9+
public function __construct()
10+
{
11+
add_action( 'admin_enqueue_scripts', [ $this, 'inline_scripts' ], 11 );
12+
}
13+
14+
public function inline_scripts()
15+
{
16+
$screen = get_current_screen();
17+
18+
$section = sanitize_text_field( wp_unslash( $_REQUEST['section'] ?? null ));
19+
20+
if ( $screen->id === 'woocommerce_page_wc-settings' && $section === 'jp_gcash_manual' ) {
21+
wp_enqueue_media();
22+
wp_enqueue_style( 'gcash_payment_gateway_for_woocommerce_css', JP_MANUAL_GCASH_URL . 'assets/admin.min.css', [], JP_MANUAL_GCASH_VER );
23+
wp_enqueue_script( 'gcash_payment_gateway_for_woocommerce_js', JP_MANUAL_GCASH_URL . 'assets/admin.min.js', [ 'jquery' ], JP_MANUAL_GCASH_VER, true );
24+
wp_localize_script( 'gcash_payment_gateway_for_woocommerce_js', 'gcash_payment_gateway_for_woocommerce_js',
25+
array(
26+
'post_url' => admin_url( 'post.php' ),
27+
)
28+
);
29+
}
30+
}
31+
32+
}
33+
34+
new JP_Manual_Gcash_Upload();

manual-gcash.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
*/
1515

1616
define( 'JP_MANUAL_GCASH_VER', '1.0' );
17-
1817
define( 'JP_MANUAL_GCASH_DIR', plugin_dir_path(__FILE__) );
18+
define( 'JP_MANUAL_GCASH_URL', plugin_dir_url(__FILE__) );
1919

2020
include plugin_dir_path(__FILE__) . '/includes/payment.php';
2121
include plugin_dir_path(__FILE__) . '/includes/order-status.php';
22-
include plugin_dir_path(__FILE__) . '/includes/reference.php';
22+
include plugin_dir_path(__FILE__) . '/includes/reference.php';
23+
include plugin_dir_path(__FILE__) . '/includes/upload.php';

0 commit comments

Comments
 (0)