From f833ae70700c87a827cb310288a810427881ac15 Mon Sep 17 00:00:00 2001 From: vendidero Date: Thu, 11 Jul 2024 11:29:41 +0200 Subject: [PATCH] Added helpers to convert images to pdf on the fly. --- .../admin/views/label/html-shipment-label.php | 4 +- includes/wc-gzd-shipment-functions.php | 1 - src/Compatibility/Bundles.php | 4 +- src/ImageToPDF.php | 56 +++++++++++++++++++ src/ShippingProvider/Auto.php | 40 ++++++++++++- src/Utilities/VariableStreamHandler.php | 46 +++++++++++++++ 6 files changed, 143 insertions(+), 8 deletions(-) create mode 100644 src/ImageToPDF.php create mode 100644 src/Utilities/VariableStreamHandler.php diff --git a/includes/admin/views/label/html-shipment-label.php b/includes/admin/views/label/html-shipment-label.php index a6c3acb4..ae14e41a 100644 --- a/includes/admin/views/label/html-shipment-label.php +++ b/includes/admin/views/label/html-shipment-label.php @@ -30,11 +30,11 @@ } $actions['delete'] = array( - 'url' => $label->get_download_url(), + 'url' => '#', 'classes' => 'remove-shipment-label delete', 'name' => _x( 'Delete label', 'shipments', 'woocommerce-germanized-shipments' ), 'action' => 'delete_label', - 'target' => '_blank', + 'target' => 'self', 'custom_attributes' => array( 'data-shipment' => $shipment->get_id(), ), diff --git a/includes/wc-gzd-shipment-functions.php b/includes/wc-gzd-shipment-functions.php index 837d7cde..8874ccb7 100644 --- a/includes/wc-gzd-shipment-functions.php +++ b/includes/wc-gzd-shipment-functions.php @@ -1432,7 +1432,6 @@ function wc_gzd_customer_return_needs_manual_confirmation( $order ) { * @return array */ function wc_gzd_get_account_shipments_actions( $shipment ) { - if ( ! is_object( $shipment ) ) { $shipment_id = absint( $shipment ); $shipment = wc_gzd_get_shipment( $shipment_id ); diff --git a/src/Compatibility/Bundles.php b/src/Compatibility/Bundles.php index a55d28ba..2b4bcc87 100644 --- a/src/Compatibility/Bundles.php +++ b/src/Compatibility/Bundles.php @@ -74,13 +74,13 @@ public static function apply_bundle_hierarchy( $shipment ) { * @return Product */ public static function get_product_from_item( $product, $item ) { - if ( ! $order = $item->get_order() || ! $product ) { + if ( ( ! $order = $item->get_order() ) || ! $product ) { return $product; } $reset_shipping_props = false; - if ( wc_pb_is_bundle_container_order_item( $item ) ) { + if ( wc_pb_is_bundle_container_order_item( $item, $order ) ) { if ( $product->needs_shipping() ) { if ( $bundle_weight = $item->get_meta( '_bundle_weight', true ) ) { if ( is_null( $bundle_weight ) ) { diff --git a/src/ImageToPDF.php b/src/ImageToPDF.php new file mode 100644 index 00000000..9b36ce30 --- /dev/null +++ b/src/ImageToPDF.php @@ -0,0 +1,56 @@ +SetMargins( 0, 0 ); + + stream_wrapper_register( 'var', '\Vendidero\Germanized\Shipments\Utilities\VariableStreamHandler' ); + } + + protected function convert_px_to_mm( $px, $dpi = 72 ) { + $pixel = 25.4 / $dpi; + + return $pixel * $px; + } + + public function import_image( $stream_or_file, $x = 0, $y = 0, $dpi = 72 ) { + if ( is_file( $stream_or_file ) ) { + $image_meta = @getimagesize( $stream_or_file ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged + + if ( ! $image_meta ) { + $this->Error( 'Invalid image data' ); + } + + $width = $this->convert_px_to_mm( $image_meta[0], $dpi ); + $height = $this->convert_px_to_mm( $image_meta[1], $dpi ); + + $this->AddPage( $this->DefOrientation, array( $width, $height ) ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase + + $this->Image( $stream_or_file, $x, $y, $width, $height ); + } else { + // Display the image contained in $data + $image_stream = 'img' . md5( $stream_or_file ); + $GLOBALS[ $image_stream ] = $stream_or_file; + $image_meta = @getimagesize( 'var://' . $image_stream ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged + + if ( ! $image_meta ) { + $this->Error( 'Invalid image data' ); + } + + $width = $this->convert_px_to_mm( $image_meta[0], $dpi ); + $height = $this->convert_px_to_mm( $image_meta[1], $dpi ); + + $this->AddPage( $this->DefOrientation, array( $width, $height ) ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase + + $type = substr( strstr( $image_meta['mime'], '/' ), 1 ); + $this->Image( 'var://' . $image_stream, 0, 0, $width, $height, $type ); + + unset( $GLOBALS[ $image_stream ] ); + } + } +} diff --git a/src/ShippingProvider/Auto.php b/src/ShippingProvider/Auto.php index 44c3d076..ea82c211 100644 --- a/src/ShippingProvider/Auto.php +++ b/src/ShippingProvider/Auto.php @@ -130,6 +130,30 @@ public function get_supported_label_reference_types( $shipment_type = 'simple' ) return array(); } + /** + * @param $ref_type + * @param $shipment_type + * + * @return array|false + */ + public function get_supported_label_reference_type( $ref_type, $shipment_type = 'simple' ) { + $ref_types = $this->get_supported_label_reference_types( $shipment_type ); + $type = false; + + if ( array_key_exists( $ref_type, $ref_types ) ) { + $type = wp_parse_args( + $ref_types[ $ref_type ], + array( + 'max_length' => -1, + 'label' => '', + 'default' => '', + ) + ); + } + + return $type; + } + /** * @param string $shipment_type * @param Label|null $label @@ -183,15 +207,25 @@ public function get_label_reference( $shipment_type = 'simple', $reference_type * @return string */ public function get_formatted_label_reference( $label, $shipment_type = 'simple', $reference_type = '' ) { - $reference = $this->get_label_reference( $shipment_type, $reference_type ); + $reference = $this->get_label_reference( $shipment_type, $reference_type ); + $max_length = -1; + + if ( $ref_type = $this->get_supported_label_reference_type( $reference_type, $shipment_type ) ) { + $max_length = $ref_type['max_length']; + } if ( ! empty( $reference ) ) { $placeholders = $this->get_label_reference_placeholders( $shipment_type, $label ); + $reference = str_replace( array_keys( $placeholders ), array_values( $placeholders ), $reference ); + } + + $formatted_ref = apply_filters( "{$this->get_hook_prefix()}formatted_label_reference", $reference, $label, $shipment_type, $reference_type ); - $reference = str_replace( array_keys( $placeholders ), array_values( $placeholders ), $reference ); + if ( -1 !== $max_length ) { + $formatted_ref = wc_gzd_shipments_substring( $formatted_ref, 0, $max_length ); } - return apply_filters( "{$this->get_hook_prefix()}formatted_label_reference", $reference, $label, $shipment_type, $reference_type ); + return $formatted_ref; } public function get_label_return_auto_enable( $context = 'view' ) { diff --git a/src/Utilities/VariableStreamHandler.php b/src/Utilities/VariableStreamHandler.php new file mode 100644 index 00000000..c3020498 --- /dev/null +++ b/src/Utilities/VariableStreamHandler.php @@ -0,0 +1,46 @@ +varname = $url['host']; + if ( ! isset( $GLOBALS[ $this->varname ] ) ) { + return false; + } + $this->position = 0; + return true; + } + + public function stream_read( $count ) { + $ret = substr( $GLOBALS[ $this->varname ], $this->position, $count ); + $this->position += strlen( $ret ); + return $ret; + } + + public function stream_eof() { + return $this->position >= strlen( $GLOBALS[ $this->varname ] ); + } + + public function stream_tell() { + return $this->position; + } + + public function stream_seek( $offset, $whence ) { + if ( SEEK_SET === $whence ) { + $this->position = $offset; + return true; + } + return false; + } + + public function stream_stat() { + return array(); + } +}