Skip to content

Commit

Permalink
Finished the implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmigf committed Mar 21, 2024
1 parent 4547ca5 commit b180669
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 75 deletions.
3 changes: 2 additions & 1 deletion assets/css/debug-tools.css
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,5 @@
#document_custom_redirect_page,
#document_custom_redirect_page + .description {
display: none;
}
}

6 changes: 6 additions & 0 deletions assets/css/settings-styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,12 @@ table.wcpdf_documents_settings_list td.title {
margin-top: 20px;
}

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

.wcpdf_document_settings_document_output_formats {
margin-bottom: 30px;
}
Expand Down
32 changes: 29 additions & 3 deletions assets/js/debug-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,18 +218,34 @@ jQuery( function( $ ) {
} );
}

// fetch numbers data
$( '#wpo-wcpdf-settings' ).on( 'click', '#fetch-numbers-data', function( e ) {
// fetch/delete number table data
$( '#wpo-wcpdf-settings' ).on( 'click', '#fetch-numbers-data, #delete-numbers-data', function( e ) {
e.preventDefault();

let table_name = $( this ).data( 'table_name' );
let $button = $( this );
let table_name = $button.data( 'table_name' );
let operation = $button.data( 'operation' );
let order = get_number_table_url_query_string( 'order' );
let orderby = get_number_table_url_query_string( 'orderby' );

// block ui
$button.closest( '.wcpdf_advanced_numbers_choose_table' ).block( {
message: null,
overlayCSS: {
background: '#fff',
opacity: 0.6
}
} );

$.ajax( {
url: wpo_wcpdf_debug.ajaxurl,
data: {
action: 'wpo_wcpdf_fetch_numbers_data',
nonce: wpo_wcpdf_debug.nonce,
table_name: table_name,
operation: operation,
order: order,
orderby: orderby,
},
type: 'POST',
success: function( response ) {
Expand All @@ -241,8 +257,18 @@ jQuery( function( $ ) {
},
error: function( xhr, ajaxOptions, thrownError ) {
alert( xhr.status + ':'+ thrownError );

$button.closest( '.wcpdf_advanced_numbers_choose_table' ).unblock();
}
} );

} );

function get_number_table_url_query_string( key ) {
let url_string = window.location.href;
let url = new URL( url_string );

return url.searchParams.get( key );
}

} );
52 changes: 39 additions & 13 deletions includes/settings/class-wcpdf-settings-debug.php
Original file line number Diff line number Diff line change
Expand Up @@ -908,7 +908,7 @@ private function get_settings_sections(): array {
) );
}

public function fetch_number_table_data( $table_name, $orderby = 'id', $order = 'DESC', $search = false ) {
public function fetch_number_table_data( $table_name, $orderby = 'id', $order = 'DESC' ) {
global $wpdb;

$chunk_size = 100;
Expand All @@ -918,12 +918,7 @@ public function fetch_number_table_data( $table_name, $orderby = 'id', $order =
$hook = 'wpo_wcpdf_number_table_data_fetch';

while ( true ) {
if ( $search ) {
$query = $wpdb->prepare( "SELECT * FROM {$table_name} WHERE `id` = %d OR `order_id` = %d ORDER BY {$orderby} {$order} LIMIT %d OFFSET %d", $search, $search, $chunk_size, $offset );
} else {
$query = $wpdb->prepare( "SELECT * FROM {$table_name} ORDER BY {$orderby} {$order} LIMIT %d OFFSET %d", $chunk_size, $offset );
}

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

if ( empty( $chunk_results ) ) {
Expand All @@ -942,7 +937,6 @@ public function fetch_number_table_data( $table_name, $orderby = 'id', $order =
'table_name' => $table_name,
'orderby' => $orderby,
'order' => $order,
'search' => $search,
'chunk_size' => $chunk_size,
'offset' => $offset,
);
Expand All @@ -956,19 +950,51 @@ public function ajax_fetch_numbers_data() {

$request = stripslashes_deep( $_POST );

if ( isset( $request['action'] ) && 'wpo_wcpdf_fetch_numbers_data' === $request['action'] && isset( $request['table_name'] ) ) {
$table_name = sanitize_text_field( $request['table_name'] );
if ( isset( $request['action'] ) && 'wpo_wcpdf_fetch_numbers_data' === $request['action'] && isset( $request['operation'] ) && isset( $request['table_name'] ) ) {
extract( $this->filter_fetch_request_data( $request ) );

delete_option( "wpo_wcpdf_number_data::{$table_name}" );
delete_option( "wpo_wcpdf_number_data::{$table_name}::last_time" );
$this->delete_number_table_data( $table_name ); // both operations require delete

$this->fetch_number_table_data( $table_name );
if ( 'fetch' === $request['operation'] ) {
$this->fetch_number_table_data( $table_name, $orderby, $order );
}

wp_send_json_success( array( esc_url_raw( admin_url( 'admin.php?page=wpo_wcpdf_options_page&tab=debug&section=numbers&orderby=id&order=asc&table_name=' . $table_name ) ) ) );
} else {
wp_send_json_error( array( __( 'Invalid request', 'woocommerce-pdf-invoices-packing-slips' ) ) );
}
}

public function filter_fetch_request_data( $request_data ) {
return array(
'table_name' => isset( $request_data['table_name'] ) && in_array( $request_data['table_name'], array_keys( $this->get_number_store_tables() ) ) ? sanitize_text_field( $request_data['table_name'] ) : null,
'order' => isset( $request_data['order'] ) && in_array( $request_data['order'], array( 'DESC', 'ASC' ) ) ? sanitize_text_field( $request_data['order'] ) : 'DESC',
'orderby' => isset( $request_data['orderby'] ) && in_array( $request_data['orderby'], array( 'id' ) ) ? sanitize_text_field( $request_data['orderby'] ) : 'id',
);
}

public function delete_number_table_data( $table_name ) {
delete_option( "wpo_wcpdf_number_data::{$table_name}" );
delete_option( "wpo_wcpdf_number_data::{$table_name}::last_time" );
}

public function search_number_in_table_data( $table_name, $search ) {
$option_name = "wpo_wcpdf_number_data::{$table_name}";
$results = get_option( $option_name, array() );
$search = ! empty( $search ) ? absint( $search ) : false;
$found = array();

if ( ! empty( $results ) ) {
foreach ( $results as $result ) {
if ( absint( $result->id ) === $search ) {
$found[] = $result;
break;
}
}
}

return $found;
}

}

Expand Down
36 changes: 8 additions & 28 deletions includes/tables/class-wcpdf-number-store-list-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,16 +180,6 @@ public function get_paged( $request ) {
return isset( $request['paged'] ) ? absint( $request['paged'] ) : 1;
}

/**
* Retrieves the search query string
*
* @since 2.0
* @return mixed string If search is present, false otherwise
*/
public function get_search( $request ) {
return ! empty( $request['s'] ) ? absint( $request['s'] ) : false;
}

/**
* Build all the number data
*
Expand All @@ -200,27 +190,14 @@ public function get_search( $request ) {
*/
public function get_numbers() {
$request = stripslashes_deep( $_GET );
$search = $this->get_search( $request );
$table_name = isset( $request['table_name'] ) && in_array( $request['table_name'], array_keys( WPO_WCPDF()->settings->debug->get_number_store_tables() ) ) ? sanitize_text_field( $request['table_name'] ) : null;
$order = isset( $request['order'] ) && in_array( $request['order'], array( 'DESC', 'ASC' ) ) ? sanitize_text_field( $request['order'] ) : 'DESC';
$orderby = isset( $request['orderby'] ) && in_array( $request['orderby'], array( 'id' ) ) ? sanitize_text_field( $request['orderby'] ) : 'id';
extract( WPO_WCPDF()->settings->debug->filter_fetch_request_data( $request ) );

$document_type = WPO_WCPDF()->settings->debug->get_document_type_from_store_table_name( $table_name );
$invoice_number_store_doc_types = WPO_WCPDF()->settings->debug->get_additional_invoice_number_store_document_types();
$document_titles = WPO_WCPDF()->documents->get_document_titles();

if ( 'invoice' !== $document_type && in_array( $document_type, $invoice_number_store_doc_types ) ) {
if ( empty( $document_type ) || ( 'invoice' !== $document_type && in_array( $document_type, $invoice_number_store_doc_types ) ) ) {
return array(); // using `invoice_number`
}

// MySQL int range
$options = array(
'options' => array(
'min_range' => 1,
'max_range' => 4294967295
)
);

$search = filter_var( $search, FILTER_VALIDATE_INT, $options );

if ( empty( $table_name ) ) {
return array();
Expand All @@ -233,8 +210,11 @@ public function get_numbers() {
$option_name = "wpo_wcpdf_number_data::{$table_name}";
$results = get_option( $option_name, array() );

// add document title or 'Deleted'
if ( ! empty( $results ) && ! empty( $document_type ) ) {
if ( ! empty( $results ) ) {
if ( isset( $request['s'] ) ) { // we have a search request, return results by search term
$results = WPO_WCPDF()->settings->debug->search_number_in_table_data( $table_name, esc_attr( $request['s'] ) );
}

foreach ( $results as $key => $result ) {
$result = (array) $result;
$document_types = array( $document_type );
Expand Down
61 changes: 31 additions & 30 deletions includes/views/advanced-numbers.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,37 +32,38 @@
?>
</p>
<p><?php _e( 'Numbers may have been assigned to orders before this.', 'woocommerce-pdf-invoices-packing-slips' ); ?></p>
<hr>
<?php
if ( ! empty( $as_actions ) ) {
echo '<div class="notice notice-info inline"><p>' . __( 'The data fetching process is currently underway. Please consider refreshing the page periodically until it is completed.', 'woocommerce-pdf-invoices-packing-slips' ) . '</p></div>';
} else {
echo '<p>' . __( '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>';

if ( ! empty( $last_fetch ) ) {
echo '<p><strong>' . __( 'Last fetch:', 'woocommerce-pdf-invoices-packing-slips' ) . '</strong> ' . date_i18n( wcpdf_date_format( null, 'number_data_last_fetch' ), $last_fetch ) . '</p>';

if ( $last_fetch > strtotime( 'today 23:59:59' ) ) {
echo '<div class="notice notice-warning inline"><p>' . __( 'The displayed data may not be current. To ensure you have the most recent information, you might want to fetch updated data.', 'woocommerce-pdf-invoices-packing-slips' ) . '</p></div>';
}
} else {
echo '<div class="notice notice-info inline"><p>' . __( 'You haven\'t fetched any data yet. Please consider doing so to view it listed below.', 'woocommerce-pdf-invoices-packing-slips' ) . '</p></div>';
}

echo '<p><a href="#" id="fetch-numbers-data" class="button button-secondary" data-table_name="' . $selected_table_name . '">' . __( 'Fetch data', 'woocommerce-pdf-invoices-packing-slips' ) . '</a></p>';
}
?>
<hr>
<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 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>
<?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 ) ) : ?>
<p><strong><?php _e( 'Last fetch:', 'woocommerce-pdf-invoices-packing-slips' ); ?> </strong><?php echo date_i18n( 'Y-m-d H:i:s', $last_fetch ); ?></p>
<?php if ( $last_fetch > strtotime( 'today 23:59:59' ) ) : ?>
<div class="notice notice-warning inline"><p><?php _e( 'The displayed data may not be current. To ensure you have the most recent information, you might want to fetch updated data.', 'woocommerce-pdf-invoices-packing-slips' ); ?></p></div>
<?php endif; ?>
<?php endif; ?>
<p>
<span><a href="#" id="fetch-numbers-data" class="button button-primary" data-table_name="<?php echo $selected_table_name; ?>" data-operation="fetch"><?php _e( 'Fetch data', 'woocommerce-pdf-invoices-packing-slips' ); ?></a></span>
<?php if ( $last_fetch ) : ?>
<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 endif; ?>
</div>
<?php
$list_table->prepare_items();
$list_table->display();
?>
<?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 b180669

Please sign in to comment.