Skip to content

Commit 221c72b

Browse files
committed
👌 IMPROVE: Updated settings image upload modal
1 parent 08040f0 commit 221c72b

4 files changed

+61
-8
lines changed

admin/class-clwc-admin.php

+2
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ public function enqueue_scripts( $hook_suffix ) {
8181
return;
8282
}
8383

84+
wp_enqueue_media();
85+
8486
wp_enqueue_script(
8587
'clwc-loyalty-ajax',
8688
plugin_dir_url( __FILE__ ) . 'js/clwc-admin.js',

admin/clwc-admin-settings.php

+26-7
Original file line numberDiff line numberDiff line change
@@ -282,20 +282,39 @@ function clwc_wysiwyg_field( $args ) {
282282
* @return void
283283
*/
284284
function clwc_image_upload_field( $args ) {
285-
$image = wp_get_attachment_url( intval( $args['value'] ) );
285+
$image_id = intval( $args['value'] );
286+
$image_url = $image_id ? wp_get_attachment_url( $image_id ) : '';
287+
288+
// Hidden field to store the image ID.
286289
printf(
287-
'<input type="hidden" name="%1$s" value="%2$s" />',
290+
'<input type="hidden" name="%1$s" value="%2$s" class="clwc-image-id" />',
288291
esc_attr( $args['name'] ),
289-
esc_attr( $args['value'] )
290-
);
291-
printf(
292-
'<img src="%s" style="max-width:150px;display:block;margin-top:10px;" /><br />',
293-
esc_url( $image )
292+
esc_attr( $image_id )
294293
);
294+
295+
// Image preview (only displayed if an image is set).
296+
if ( $image_url ) {
297+
printf(
298+
'<img src="%s" style="max-width:150px;display:block;margin-top:10px;" class="clwc-image-preview" />',
299+
esc_url( $image_url )
300+
);
301+
} else {
302+
// Placeholder (or hidden image preview if no image is set).
303+
echo '<img src="" style="max-width:150px;display:none;margin-top:10px;" class="clwc-image-preview" />';
304+
}
305+
306+
// Button to open media uploader.
295307
printf(
296308
'<button type="button" class="button clwc-upload-image-button">%s</button>',
297309
esc_html__( 'Upload Image', 'customer-loyalty-for-woocommerce' )
298310
);
311+
312+
// Button to remove image.
313+
printf(
314+
'<button type="button" class="button clwc-remove-image-button" style="display:%s;">%s</button>',
315+
$image_url ? 'inline-block' : 'none',
316+
esc_html__( 'Remove Image', 'customer-loyalty-for-woocommerce' )
317+
);
299318
}
300319

301320
/**

admin/js/clwc-admin.js

+32
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,36 @@ jQuery(document).ready(function($) {
3939
}
4040
});
4141
});
42+
43+
var file_frame;
44+
45+
$(document).on('click', '.clwc-upload-image-button', function(e) {
46+
e.preventDefault();
47+
48+
// If the media frame already exists, reopen it
49+
if (file_frame) {
50+
file_frame.open();
51+
return;
52+
}
53+
54+
// Create a new media frame
55+
file_frame = wp.media.frames.file_frame = wp.media({
56+
title: 'Select or Upload an Image',
57+
button: {
58+
text: 'Use this image',
59+
},
60+
multiple: false // Set to true if you want multiple files
61+
});
62+
63+
// When an image is selected, run a callback
64+
file_frame.on('select', function() {
65+
var attachment = file_frame.state().get('selection').first().toJSON();
66+
$('.clwc-upload-image-button').prev('input[type="hidden"]').val(attachment.id);
67+
$('.clwc-upload-image-button').prev().prev('img').attr('src', attachment.url).show();
68+
});
69+
70+
// Finally, open the modal
71+
file_frame.open();
72+
});
73+
4274
});

customer-loyalty-for-woocommerce.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
function clwc_create_loyalty_log_table() {
4545
global $wpdb;
4646

47-
// Define table name with the WordPress table prefix.
47+
// Define table name with the WordPress® table prefix.
4848
$table_name = $wpdb->prefix . 'clwc_loyalty_log';
4949

5050
// Set the database character set and collation for security.

0 commit comments

Comments
 (0)