Skip to content

Commit

Permalink
Add Ajax fetch document data
Browse files Browse the repository at this point in the history
  • Loading branch information
MohamadNateqi committed Apr 29, 2024
1 parent a4b723e commit be12ee7
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 2 deletions.
50 changes: 49 additions & 1 deletion assets/js/order-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,5 +187,53 @@ jQuery( function( $ ) {
$( '.view-more' ).show();
}
} );


function get_pending_documents() {
let pending_documents = [];
$( '.wcpdf-data-fields' ).each( function () {
if ( 'yes' === $( this ).attr( 'data-is_pending' ) ) {
pending_documents.push( $( this ).data( 'document' ) );
}
} );

return pending_documents;
}

// Fetch data for pending documents if documents were pending and now are generated.
const pending_documents = get_pending_documents();
let ajax_count = 0;
const ajax_max_count = 3;
const ajax_interval = 2000;
const ajax_timer = function() {
if ( pending_documents.length <= 0 ) {
return;
}

$.ajax( {
url: wpo_wcpdf_ajax.ajaxurl,
type: 'POST',
data: {
action: 'wpo_fetch_document_data',
security: wpo_wcpdf_ajax.nonce,
document_types: pending_documents,
order_id: woocommerce_admin_meta_boxes.post_id,
},
success: function ( response ) {
$.each( response.data, function ( key, value ) {
$( '.wcpdf-data-fields[data-document="' + key + '"]' ).replaceWith( value );
} );
},
error: function ( response ) {
console.log( response.message );
}
} );

ajax_count++;
if ( ajax_count < ajax_max_count ) {
setTimeout( ajax_timer, ajax_interval );
}
}

setTimeout( ajax_timer, ajax_interval );

} );
50 changes: 49 additions & 1 deletion includes/class-wcpdf-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,12 @@ public function __construct() {
// document actions
add_action( 'wpo_wcpdf_document_actions', array( $this, 'add_regenerate_document_button' ) );

// add "invoice number" column to WooCommerce Analytic - Orders
// add "invoice number" column to WooCommerce Analytic - Orders
add_filter( 'woocommerce_rest_prepare_report_orders', array( $this, 'add_invoice_number_to_order_report' ) );
add_filter( 'woocommerce_report_orders_export_columns', array( $this, 'add_invoice_number_header_to_order_export' ) );
add_filter( 'woocommerce_report_orders_prepare_export_item', array( $this, 'add_invoice_number_value_to_order_export' ), 10, 2 );

add_action( 'wp_ajax_wpo_fetch_document_data', array( $this, 'ajax_fetch_pdf_document_data' ) );
}

// display review admin notice after 100 pdf downloads
Expand Down Expand Up @@ -1279,6 +1281,52 @@ private function is_invoice_number_numeric() {
return apply_filters( 'wpo_wcpdf_invoice_number_is_numeric', $is_numeric );
}

/**
* Updates documents data in the "PDF document data" meta box if the generation in the background is finished.
*
* @return void
*/
public function ajax_fetch_pdf_document_data(): void {
if ( ! isset( $_REQUEST['security'] ) || ! wp_verify_nonce( $_REQUEST['security'], 'generate_wpo_wcpdf' ) ) {
wp_send_json_error( array(
'message' => esc_html__( 'Invalid or expired nonce!', 'woocommerce-pdf-invoices-packing-slips' ),
) );
}

if ( empty( $_REQUEST['document_types'] ) || empty( $_REQUEST['order_id'] ) ) {
wp_send_json_error( array(
'message' => esc_html__( 'Incomplete or incorrect request!', 'woocommerce-pdf-invoices-packing-slips' ),
) );
}

$order_id = (int) $_REQUEST['order_id'];
$document_types = array_map( 'sanitize_text_field', $_REQUEST['document_types'] );
$documents_data = array();

ob_start();
foreach ( $document_types as $document_type ) {
if ( ! wpo_wcpdf_is_document_type_valid( $document_type ) ) {
continue;
}

$document = wcpdf_get_document( $document_type, wc_get_order( $order_id ) );
if ( $document && $document->exists() ) {
$this->output_number_date_edit_fields( $document );
$documents_data[ $document_type ] = ob_get_contents();
ob_clean();
}
}
ob_end_clean();

if ( ! empty( $documents_data ) ) {
wp_send_json_success( $documents_data );
}

wp_send_json_error( array(
'message' => esc_html__( 'Documents data is empty!', 'woocommerce-pdf-invoices-packing-slips' ),
) );
}

}

endif; // class_exists
18 changes: 18 additions & 0 deletions includes/wcpdf-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -675,3 +675,21 @@ function wpo_wcpdf_parse_document_date_for_wp_query( array $wp_query_args, array
return $wp_query_args;
}

/**
* Validates the given document type against the currently defined documents.
*
* @param string $document_type
*
* @return bool
*/
function wpo_wcpdf_is_document_type_valid( string $document_type ): bool {
$documents = WPO_WCPDF()->documents->get_documents();

foreach ( $documents as $document ) {
if ( $document_type === $document->get_type() ) {
return true;
}
}

return false;
}

0 comments on commit be12ee7

Please sign in to comment.