Skip to content

Commit a490566

Browse files
committed
Refactor magic URL setup to optimize link object fetching and expiration formatting
- Cached link objects to improve performance and reduce redundant API calls. - Simplified expiration date formatting logic for better readability and maintainability. - Removed unused variable for link object ID, streamlining the code. - Enhanced data attributes for expiration display, ensuring accurate and user-friendly output.
1 parent 9c6d519 commit a490566

1 file changed

Lines changed: 13 additions & 13 deletions

File tree

dt-reports/magic-url-setup.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -331,11 +331,7 @@ function app_link_reset(evt) {
331331

332332
const button = modalContent.querySelector('.button');
333333
const loadingSpinner = modalContent.querySelector('.loading-spinner');
334-
335-
// Store old hash before reset
336-
const appAccordion = evt.target.closest('.app-accordion');
337-
const oldHash = url ? url.replace(appAccordion.dataset.urlBase, '') : '';
338-
334+
339335
button.addEventListener('click', function () {
340336
this.disabled = true;
341337
loadingSpinner.classList.add('active');
@@ -640,18 +636,20 @@ class_exists( 'Disciple_Tools_Bulk_Magic_Link_Sender_API' )
640636
&& method_exists( 'Disciple_Tools_Bulk_Magic_Link_Sender_API', 'capture_expiry_details' )
641637
);
642638

643-
$link_obj_id = null;
644-
645639
if ( $magic_link_expiration_supported ) {
646640
// Try to find matching link_obj for backward compatibility
647641
$matching_link_obj = null;
648-
$link_objs = Disciple_Tools_Bulk_Magic_Link_Sender_API::fetch_option_link_objs();
642+
643+
static $cached_link_objs = null;
644+
if ( is_null( $cached_link_objs ) ) {
645+
$cached_link_objs = Disciple_Tools_Bulk_Magic_Link_Sender_API::fetch_option_link_objs();
646+
}
647+
$link_objs = $cached_link_objs;
649648

650649
foreach ( $link_objs as $link_obj ) {
651650
$generated_key = Disciple_Tools_Bulk_Magic_Link_Sender_API::generate_magic_link_type_key( $link_obj );
652651
if ( $generated_key === $meta_key ) {
653652
$matching_link_obj = $link_obj;
654-
$link_obj_id = $link_obj->id;
655653
break;
656654
}
657655
}
@@ -664,6 +662,11 @@ class_exists( 'Disciple_Tools_Bulk_Magic_Link_Sender_API' )
664662
$matching_link_obj
665663
);
666664
}
665+
666+
// Prepare short expiration format using site date/time settings
667+
$date_time_format = get_option( 'date_format' ) . ' ' . get_option( 'time_format' );
668+
$exp_ts = $expiration_data['ts'] ?? '';
669+
$exp_short = ! empty( $exp_ts ) ? date_i18n( $date_time_format, (int) $exp_ts ) : '';
667670
?>
668671
<details class="app-accordion <?php echo esc_attr( $meta_key ); ?>"
669672
data-url-base="<?php echo esc_url( $app_url_base ) ?>"
@@ -673,19 +676,16 @@ class_exists( 'Disciple_Tools_Bulk_Magic_Link_Sender_API' )
673676
data-root="<?php echo esc_attr( $app['root'] ) ?>"
674677
data-type="<?php echo esc_attr( $app['type'] ) ?>"
675678
data-key="<?php echo esc_attr( $app['meta_key'] ) ?>"
676-
data-link-obj-id="<?php echo esc_attr( $link_obj_id ?? '' ) ?>"
677679
data-sys-type="<?php echo esc_attr( $sys_type ) ?>"
678680
data-record-id="<?php echo esc_attr( $record_id ) ?>"
679681
data-expires-ts="<?php echo esc_attr( $expiration_data['ts'] ?? '' ) ?>"
680682
data-expires-formatted="<?php echo esc_attr( $expiration_data['ts_formatted'] ?? '---' ) ?>"
681-
data-expires-formatted-short="<?php echo esc_attr( ! empty( $expiration_data['ts'] ) ? date_i18n( 'n/j/y G:i', (int) $expiration_data['ts'] ) : '' ) ?>"
683+
data-expires-formatted-short="<?php echo esc_attr( $exp_short ) ?>"
682684
>
683685
<summary class="app-summary">
684686
<div class="app-label" style="display: flex; flex-direction: column; align-items: flex-start;">
685687
<span class="app-label-title"><?php echo esc_html( $app['label'] ) ?></span>
686688
<?php
687-
$exp_ts = $expiration_data['ts'] ?? '';
688-
$exp_short = ! empty( $exp_ts ) ? date_i18n( 'n/j/y G:i', (int) $exp_ts ) : '';
689689
if ( ! empty( $exp_short ) ) :
690690
?>
691691
<span class="app-expiration-badge" style="font-size: 0.85em; color: #666; margin-top: 0.25rem;">

0 commit comments

Comments
 (0)