Skip to content

Commit

Permalink
Merge PR #195
Browse files Browse the repository at this point in the history
  • Loading branch information
sal4sup committed Dec 1, 2024
2 parents 30b56a4 + fc8b293 commit 965f3cc
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 38 deletions.
41 changes: 9 additions & 32 deletions src/Order/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -543,14 +543,7 @@ function ( $barc ) {
$barcodes
);

$saved_data['labels'] = array_map(
function ( $label ) {
unset( $label['merged_files'] );

return $label;
},
$labels
);
$saved_data['labels'] = $labels;

if ( $this->settings->is_auto_complete_order_enabled() ) {
// Updating the order status to completed.
Expand Down Expand Up @@ -717,7 +710,8 @@ public function put_label_content( $response, $order, $parent_barcode, $parent_l
$label_extension = ! empty( $label_contents['OutputType'] ) ? sanitize_title( $label_contents['OutputType'] ) : 'pdf';
$barcode = $response[ $type ][ $shipment_idx ][ $content_type['barcode_key'] ];
$barcode = is_array( $barcode ) ? array_shift( $barcode ) : $barcode;
$filename = Utils::generate_label_name( $order->get_id(), $label_type, $barcode, 'A6', $label_extension );
$label_format = $this->settings->get_label_format();
$filename = Utils::generate_label_name( $order->get_id(), $label_type, $barcode, $label_format, $label_extension );
$filepath = trailingslashit( POSTNL_UPLOADS_DIR ) . $filename;

if ( wp_mkdir_p( POSTNL_UPLOADS_DIR ) && ! file_exists( $filepath ) ) {
Expand Down Expand Up @@ -1283,6 +1277,12 @@ public function delete_label( $saved_data ) {
return false;
}

if ( isset( $saved_data['labels']['label']['merged_files'] ) ) {
foreach ( $saved_data['labels']['label']['merged_files'] as $label_path ) {
unlink( $label_path );
}
}

return unlink( $saved_data['labels']['label']['filepath'] );
}

Expand Down Expand Up @@ -1425,29 +1425,6 @@ protected function get_tracking_link( $order_id ) {
return sprintf( '<a href="%1$s" target="_blank" class="postnl-tracking-link">%2$s</a>', esc_url( $tracking_url ), $saved_data['labels']['label']['barcode'] );
}

/**
* Delete label files from label info.
*
* @param Array $labels List of label info.
*/
public function delete_label_files( $labels ) {
if ( empty( $labels ) ) {
return;
}

foreach ( $labels as $label_type => $label_info ) {
if ( empty( $label_info['merged_files'] ) || empty( $label_info['filepath'] ) ) {
continue;
}

foreach ( $label_info['merged_files'] as $path ) {
if ( file_exists( $path ) && $path !== $label_info['filepath'] ) {
unlink( $path );
}
}
}
}

/**
* Check if the order have the label data.
*
Expand Down
4 changes: 0 additions & 4 deletions src/Order/Bulk.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,6 @@ public function merge_bulk_labels( $gen_labels ) {

$merged_info = $this->merge_labels( $label_paths, $filename, $start_position );

foreach ( $gen_labels as $labels ) {
$this->delete_label_files( $labels );
}

if ( file_exists( $merged_info ['filepath'] ) ) {
// We're saving the bulk file path temporarily and access it later during the download process.
// This information expires in 3 minutes (180 seconds), just enough for the user to see the
Expand Down
2 changes: 0 additions & 2 deletions src/Order/Single.php
Original file line number Diff line number Diff line change
Expand Up @@ -460,8 +460,6 @@ public function save_meta_box_ajax() {
$labels = $result['labels'];
$tracking_note = $this->get_tracking_note( $order_id );

$this->delete_label_files( $labels );

if ( ! empty( $tracking_note ) ) {
$return_data = array_merge(
$result['saved_data'],
Expand Down

0 comments on commit 965f3cc

Please sign in to comment.