Skip to content

Commit 4fbc860

Browse files
* If Clipboard API isn't supported remove all copy to clipboard buttons from the DOM
* Add further buttons to allow copy to clibboard order ids, subscriptions id, payment and transaction ids.
1 parent 9b3d31a commit 4fbc860

File tree

5 files changed

+59
-19
lines changed

5 files changed

+59
-19
lines changed

classes/class-pmpro-discount-code-list-table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ public function column_discount_code( $item ) {
370370
?>
371371
<strong><a title="<?php echo esc_attr( sprintf( __( 'Edit Code: %s', 'paid-memberships-pro' ), $item->id ) ); ?>" href="<?php echo esc_url( add_query_arg( array( 'page' => 'pmpro-discountcodes', 'edit' => $item->id ), admin_url('admin.php' ) ) ); ?>"><?php echo esc_html( $item->code ); ?></a></strong>
372372
<button title="<?php echo esc_attr__('Copy code to the clipboard', 'paid-memberships-pro' ) ?>" type="button"
373-
class="pmpro_copy_discount_code button-link edit-filters" style="display:none">
373+
class="pmpro_copy_discount_code pmpro_copy_to_clipboard button-link edit-filters" style="display:none">
374374
<span class="dashicons dashicons-clipboard" aria-hidden="true"></span>
375375
</button>
376376
<div class="row-actions">

classes/class-pmpro-orders-list-table.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -891,6 +891,10 @@ public function column_order_id( $item ) {
891891
public function column_order_code( $item ) {
892892
?>
893893
<strong><a href="admin.php?page=pmpro-orders&order=<?php echo esc_attr( $item->id ); ?>"><?php echo esc_html( $item->code ); ?></a></strong>
894+
<button title="<?php echo esc_attr__('Copy code to the clipboard', 'paid-memberships-pro' ) ?>" type="button"
895+
class="pmpro_copy_order_id pmpro_copy_to_clipboard button-link edit-filters" style="display:block">
896+
<span class="dashicons dashicons-clipboard" aria-hidden="true"></span>
897+
</button>
894898
<div class="row-actions">
895899
<?php
896900
$delete_text = esc_html(
@@ -1230,7 +1234,18 @@ public function column_transaction_ids( $item ) {
12301234

12311235
// Echo the data for this column.
12321236
foreach( $column_value as $key => $value ) {
1233-
echo '<p>' . wp_kses_post( $value ) . '</p>';
1237+
echo '<p>' .
1238+
wp_kses_post( $value );
1239+
//we don't want to show the copy button for the empty column
1240+
if( $key !== 'none') {
1241+
?>
1242+
<button title="<?php echo esc_attr( sprintf( __( 'Copy the %s to the clipboard', 'paid-memberships-pro' ), $key ) ) ?>" type="button"
1243+
class="pmpro_copy_<? echo esc_attr( $key ) ?> pmpro_copy_to_clipboard button-link edit-filters" style="display:none">
1244+
<span class="dashicons dashicons-clipboard" aria-hidden="true"></span>
1245+
</button>
1246+
<?php
1247+
}
1248+
echo '</p>';
12341249
}
12351250
}
12361251

classes/class-pmpro-subscriptions-list-table.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,10 @@ public function column_default( $item, $column_name ) {
449449
public function column_id( $item ) {
450450
?>
451451
<strong><a href="admin.php?page=pmpro-subscriptions&id=<?php echo esc_attr( $item->get_id() ); ?>"><?php echo esc_html( $item->get_subscription_transaction_id() ); ?></a></strong>
452+
<button title="<?php echo esc_attr__('Copy subscription ID to the clipboard', 'paid-memberships-pro' ) ?>" type="button"
453+
class="pmpro_copy_subscription_id pmpro_copy_to_clipboard button-link edit-filters" style="display:none">
454+
<span class="dashicons dashicons-clipboard" aria-hidden="true"></span>
455+
</button>
452456
<div class="row-actions">
453457
<?php
454458
$actions = [

css/admin.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1647,13 +1647,13 @@ table.pmprommpu_levels tr.remove_level td {background: #F2DEDE; }
16471647
-moz-osx-font-smoothing: grayscale;
16481648
}
16491649

1650-
.pmpro_admin tr td button.pmpro_copy_discount_code .dashicons:not(.button .dashicons) {
1650+
.pmpro_admin tr td button.pmpro_copy_to_clipboard .dashicons:not(.button .dashicons) {
16511651
padding-top: unset;
16521652
font-size: 14px;
16531653
line-height: 1.4;
16541654
}
16551655

1656-
.pmpro_admin tr td button.pmpro_copy_discount_code:hover {
1656+
.pmpro_admin tr td button.pmpro_copy_to_clipboard:hover {
16571657
color: #043959;
16581658
}
16591659

js/pmpro-admin.js

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,19 +1003,41 @@ jQuery(document).ready(function () {
10031003
jQuery('.pmpro_admin-pmpro-orders select#membership_id').select2();
10041004
});
10051005

1006+
/**
1007+
* Copy to the clipboard for discount codes, order ids, payment ids, etc.
1008+
*/
10061009
jQuery(document).ready(function ($) {
10071010

1011+
//get all the buttons
1012+
const $allButtons = $( '.pmpro_copy_to_clipboard' );
1013+
1014+
//get all table rows
1015+
const $allTableRows = $( 'table.discountcodes tr, table.subscriptions tr, table.orders tr' );
1016+
1017+
//get all anchors in the same table cell
1018+
const $allAnchors = $( 'td.column-discount_code a, td.column-subscription_id a, td.column-order_id a' );
1019+
1020+
// Bail if Clipboard API isn't supported
1021+
if ( ! navigator.clipboard ) {
1022+
//hide all buttons. Iterate over the jquery elements array and hide them all.
1023+
$allButtons.each( function () {
1024+
$( this ).remove();
1025+
});
1026+
}
1027+
10081028
/**
10091029
* If it's mouseenter show the clipboard icon hider otherwise
10101030
*
10111031
* @param {Event} evt The event object
10121032
* @returns {void}
10131033
* @since TBD
10141034
*/
1015-
$('table.discountcodes tr').on( "mouseenter mouseleave focus", function ( evt ) {
1016-
$(this).find('td.column-discount_code .pmpro_copy_discount_code').hide();
1017-
if (evt.type === 'mouseenter' ) {
1018-
$(this).find('.pmpro_copy_discount_code').show();
1035+
$( $allTableRows ).on( "mouseenter mouseleave focus", function ( evt ) {
1036+
const button = $( this ).find( '.pmpro_copy_to_clipboard' );
1037+
button.hide();
1038+
1039+
if ( evt.type === 'mouseenter' || evt.type === 'focus' ) {
1040+
button.show();
10191041
}
10201042
});
10211043

@@ -1026,25 +1048,24 @@ jQuery(document).ready(function ($) {
10261048
* @returns {void}
10271049
* @since TBD
10281050
*/
1029-
$('td.column-discount_code a').on( "focus", function ( evt ) {
1030-
$(this).closest( 'td' ).find( '.pmpro_copy_discount_code' ).show();
1051+
$( $allAnchors ).on( "focus", function ( evt ) {
1052+
$(this).closest('td').find('.pmpro_copy_to_clipboard').show();
10311053
});
10321054

10331055
/**
1034-
* Copy Discount Code to Clipboard
1056+
* Copy to Clipboard different codes across the site. Check the class pmpro_copy_to_clipboard to find where it's used.
10351057
*
10361058
* @param {Event} evt The click event
10371059
* @returns {void}
10381060
* @since TBD
10391061
*/
1040-
$('.pmpro_copy_discount_code').on('click', function ( evt ) {
1062+
$('.pmpro_copy_to_clipboard').on('click', function ( evt ) {
10411063
// Find first link text
1042-
const code = $(this).closest('td').find('a').first().text();
1043-
1044-
// Bail if Clipboard API isn't supported
1045-
if ( ! navigator.clipboard ) {
1046-
return;
1047-
}
1064+
let code = $(this).closest('td').find('a').first().text();
1065+
//if it's a payment transaction id, we need to get the text from the first anchor
1066+
if( $(this).hasClass('pmpro_copy_payment_transaction_id') ) {
1067+
code = $(this).closest('td').find('p').first().text().split(":")[1].trim();
1068+
}
10481069

10491070
// Create a new Blob object with the discount code
10501071
const blob = new Blob([code], { type: 'text/plain' });
@@ -1059,7 +1080,7 @@ jQuery(document).ready(function ($) {
10591080
// Remove success message and show copy button after a delay
10601081
setTimeout(() => {
10611082
$('.success-message').remove();
1062-
$('.pmpro_copy_discount_code').show();
1083+
$('.pmpro_copy_to_clipboard').show();
10631084
}, 3000);
10641085
})
10651086
.catch(err => {

0 commit comments

Comments
 (0)