Skip to content

Commit

Permalink
Tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmigf committed Mar 26, 2024
1 parent a0d366a commit ed2da3a
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 50 deletions.
1 change: 0 additions & 1 deletion assets/css/settings-styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,6 @@ table.wcpdf_documents_settings_list td.title {

.wcpdf_advanced_numbers_choose_table .number-table-data-info {
border-top: 1px solid #eaeaea;
border-bottom: 1px solid #eaeaea;
margin-bottom: 20px;
}

Expand Down
69 changes: 34 additions & 35 deletions includes/settings/class-wcpdf-settings-debug.php
Original file line number Diff line number Diff line change
Expand Up @@ -938,47 +938,46 @@ public function fetch_number_table_data( string $table_name, string $orderby = '

$total_limit = 'all' !== $data['limit'] ? absint( $data['limit'] ) : 0;
$total_fetched = absint( $total_fetched ?? 0 );
$chunk_size = 0 !== $total_limit && $total_limit < $chunk_size ? min( $total_limit - $offset, $chunk_size ) : absint( $chunk_size ?? 100 );
$offset = absint( $offset ?? 0 );
$chunk_size = 0 !== $total_limit ? min( $total_limit - $offset, $chunk_size ) : absint( $chunk_size ?? 100 );
$option_name = "wpo_wcpdf_number_data::{$data['table_name']}";
$results = get_option( $option_name, array() );
$hook = 'wpo_wcpdf_number_table_data_fetch';

while ( true ) {
$query = $wpdb->prepare( "SELECT * FROM {$data['table_name']} ORDER BY {$data['orderby']} {$data['order']} LIMIT %d OFFSET %d", $chunk_size, $offset );
$chunk_results = $wpdb->get_results( $query );

if ( empty( $chunk_results ) ) {
as_unschedule_all_actions( $hook );
update_option( $option_name . '::last_time', time() );
break; // exit the loop if no more results
}

$results = array_merge( $results, $chunk_results ); // append the chunk results to the main results array
$total_fetched += count( $chunk_results ); // update total fetched rows

update_option( $option_name, $results );

if ( 0 !== $total_limit && $total_fetched >= $total_limit ) {
as_unschedule_all_actions( $hook );
update_option( $option_name . '::last_time', time() );
break; // exit the loop if total limit is reached
}

$offset += $chunk_size; // increase the offset for the next chunk

$args = array(
'table_name' => $data['table_name'],
'orderby' => $data['orderby'],
'order' => $data['order'],
'limit' => $data['limit'],
'chunk_size' => $chunk_size,
'offset' => $offset,
'total_fetched' => $total_fetched,
);

as_enqueue_async_action( $hook, $args );
// query
$query = $wpdb->prepare( "SELECT * FROM {$data['table_name']} ORDER BY {$data['orderby']} {$data['order']} LIMIT %d OFFSET %d", $chunk_size, $offset );
$chunk_results = $wpdb->get_results( $query );

if ( empty( $chunk_results ) ) {
as_unschedule_all_actions( $hook );
update_option( $option_name . '::last_time', time() );
return; // exit the loop if no more results
}

$results = array_merge( $results, $chunk_results ); // append the chunk results to the main results array
$total_fetched += count( $chunk_results ); // update total fetched rows

update_option( $option_name, $results );

if ( 0 !== $total_limit && $total_fetched >= $total_limit ) {
as_unschedule_all_actions( $hook );
update_option( $option_name . '::last_time', time() );
return; // exit the loop if total limit is reached
}

$offset += $chunk_size; // increase the offset for the next chunk

$args = array(
'table_name' => $data['table_name'],
'orderby' => $data['orderby'],
'order' => $data['order'],
'limit' => $data['limit'],
'chunk_size' => $chunk_size,
'offset' => $offset,
'total_fetched' => $total_fetched,
);

as_enqueue_async_action( $hook, $args );
}

/**
Expand Down
39 changes: 25 additions & 14 deletions includes/views/advanced-numbers.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,18 @@
<p><?php _e( 'Numbers may have been assigned to orders before this.', 'woocommerce-pdf-invoices-packing-slips' ); ?></p>
<div class="number-table-data-info">
<?php if ( ! empty( $as_actions ) ) : ?>
<div class="notice notice-info inline"><p><?php _e( 'The data fetching process is currently underway. Please consider refreshing the page periodically until it is completed.', 'woocommerce-pdf-invoices-packing-slips' ); ?></p></div>
<div class="notice notice-info inline">
<p>
<?php
printf(
/* translators: %1$s: link to action scheduler, %2$s: closing tag */
__( 'The data fetching process is currently underway. Please consider refreshing the page periodically until it is completed or check current status %1$shere%2$s', 'woocommerce-pdf-invoices-packing-slips' ),
'<a href="' . esc_url( admin_url( 'admin.php?page=wc-status&tab=action-scheduler&s=wpo_wcpdf_number_table_data_fetch' ) ) . '">',
'</a>'
);
?>
</p>
</div>
<?php else : ?>
<p><?php _e( 'Given the potential impact of querying a large volume of orders on site performance, it\'s essential to fetch data each time you need the most current information. This procedure ensures that the site remains efficient and responsive, even when handling substantial order quantities.', 'woocommerce-pdf-invoices-packing-slips' ); ?></p>
<?php if ( ! empty( $last_fetch ) ) : ?>
Expand Down Expand Up @@ -75,21 +86,21 @@
<span style="margin-left:6px;"><a href="#" id="delete-numbers-data" class="button button-secondary" data-table_name="<?php echo $selected_table_name; ?>" data-operation="delete"><?php _e( 'Delete cached data', 'woocommerce-pdf-invoices-packing-slips' ); ?></a></span>
<?php endif; ?>
</p>
<?php if ( $last_fetch ) : ?>
<div class="number-search" style="text-align:right;">
<input type="number" id="number_search_input" name="number_search_input" min="1" max="4294967295" value="<?php echo isset( $_REQUEST['s'] ) ? esc_attr( $_REQUEST['s'] ) : ''; ?>">
<a href="#" class="button button-primary number-search-button"><?php _e( 'Search number', 'woocommerce-pdf-invoices-packing-slips' ); ?></a>
<?php $disabled = ( isset( $_REQUEST['s'] ) && ! empty( $_REQUEST['s'] ) ) ? '' : 'disabled'; ?>
<a href="<?php echo esc_url( remove_query_arg( 's' ) ); ?>" class="button button-secondary" <?php echo $disabled; ?>><?php _e( 'Reset', 'woocommerce-pdf-invoices-packing-slips' ); ?></a>
</div>
<?php $list_table->prepare_items(); $list_table->display(); ?>
<?php else : ?>
<div class="notice notice-info inline">
<p><?php _e( 'Please fetch data to view it listed here.', 'woocommerce-pdf-invoices-packing-slips' ); ?></p>
</div>
<?php endif; ?>
<?php endif; ?>
</div>
<?php if ( $last_fetch ) : ?>
<div class="number-search" style="text-align:right;">
<input type="number" id="number_search_input" name="number_search_input" min="1" max="4294967295" value="<?php echo isset( $_REQUEST['s'] ) ? esc_attr( $_REQUEST['s'] ) : ''; ?>">
<a href="#" class="button button-primary number-search-button"><?php _e( 'Search number', 'woocommerce-pdf-invoices-packing-slips' ); ?></a>
<?php $disabled = ( isset( $_REQUEST['s'] ) && ! empty( $_REQUEST['s'] ) ) ? '' : 'disabled'; ?>
<a href="<?php echo esc_url( remove_query_arg( 's' ) ); ?>" class="button button-secondary" <?php echo $disabled; ?>><?php _e( 'Reset', 'woocommerce-pdf-invoices-packing-slips' ); ?></a>
</div>
<?php $list_table->prepare_items(); $list_table->display(); ?>
<?php else : ?>
<div class="notice notice-info inline">
<p><?php _e( 'Please fetch data to view it listed here.', 'woocommerce-pdf-invoices-packing-slips' ); ?></p>
</div>
<?php endif; ?>
<?php else : ?>
<div class="notice notice-info inline">
<p><?php _e( 'Please select a number store!', 'woocommerce-pdf-invoices-packing-slips' ); ?></p>
Expand Down

0 comments on commit ed2da3a

Please sign in to comment.